除了Python之外,意外使用了try

时间:2015-12-18 21:28:46

标签: python

我正在学习python,我试图制作一个只有问题的房间类型的游戏。 我在我的ubuntu 14.04上使用python 2.7

有两种方法可以完成游戏而没有死路。

  

bb_room-> flats_room-> brass_or_wood->零件 - >斯塔夫罗斯

  

bb_room-> sharps_room->零件 - >斯塔夫罗斯

我发布了整个代码,我有一些问题:

第一

如果我选择了第一条路径,当我到达stavros房间并回答一个号码并打印出正确答案时,我点击后输入它要求我

flats or sharps

如果你回答bb_room的第一个问题,这就是问题。

当我选择第二条路径时,我完成并点击上面的输入,它要求我

brass or woodwind

我无法理解这里发生了什么!

第二次

为什么当我再次在stavros房间并给出一个字母而不是一个整数时,它会打印

Oops! That was no valid number. Try again...

Press Enter to continue...

然后重新开始。此时我没有使用try exceptstavros会议室。)

from sys import exit
import webbrowser
import os


def bb_room():
    print ("Play the Bb major \n(write the positions with spaces between)")
    positions=raw_input(prompt)
    positions_list=positions.split()
    try:
        positions_list=[int(a) for a in positions_list]
        if positions_list == [1,6,4,3,1,4,2,1]:
            print "\nYou right.."
            print "Do you like flats or sharps?"

            a=0
            while 1>a:
                fors=raw_input(prompt)
                fors=fors.lower()
                if fors=="flats":                   
                    flats_room()
                elif fors=="sharps":                    
                    sharps_room()
                else:
                    print "Choose flats or sharps"

        else:
            dead("You don't know the basics.")
    except ValueError:
        print "Oops!  That was no valid number.  Try again..."
        raw_input("Press Enter to continue...")
        os.system('clear')
        start()

def flats_room():
    print ("\nHow many flats Db major has?")    
    flats=raw_input(prompt)
    flats=flats.lower()
    if flats=="5" or flats=='five':
        print "That's right!"
        brass_or_wood()
    else:
        dead("You should learn some music theory.")

def sharps_room():
    print ("\nHow many sharps E major has?")
    sharps=raw_input(prompt)
    sharps=sharps.lower()
    if sharps=="4" or sharps=='four':
        print "That's right!"
        parts()
    else:
        dead("You should learn some music theory.") 

def brass_or_wood():
    print ("\nWhat kind of instrument a trombone is?\nBrass or Windwood?")  
    a=0
    while 1>a:
        bow=raw_input(prompt)
        bow=bow.lower()
        if bow=="brass":
            print "That's right!"
            parts()
        elif bow=="windwood":
            webbrowser.open("https://www.youtube.com/watch?v=X2WH8mHJnhM",new=0,autoraise=True)
            dead("Go play some recorder!")      
        else:
            print "Choose Brass or Windwood!"

def parts():
    print ("\nHow many parts a trombone has?")  
    parts=raw_input(prompt)
    parts=parts.lower()
    if parts=="4" or parts=='four':
        print "That's right!"
        stavros()
    else:
        dead("You should learn some stuff about the bone.") 

def stavros():
    print ("\nYou meet Stavros at Politistiko and of course you are unprepared\nfor your lesson. How many points you lose?")    
    points=raw_input(prompt)
    points=int(points)
    if points > 0 and points <100:
        print ("You guessed right. %d is the right amount you deserve to loose" % points)           
    elif points < 0:
        print ("You can't lose a negative number cause you 'll add it!")            
    elif points >100:
        print ("Don't be so hard with yourself. Stavros is not a bad guy")          
    else:
        print ("You should put a number")

def start():
    print ("Welcome to the Trombone game.\n\n") 
    bb_room()

def dead(why):
    print why, "Start practising!"
    raw_input("Press Enter to continue...")
    os.system('clear')
    start()

prompt="> "
start()

1 个答案:

答案 0 :(得分:0)

首先:

stavros中调用<parts来调用brass_or_wood。在brass_or_wood中,您有一个无限循环,a=0while 1>a(顺便说一下,您可以简化为while 1:)。你永远不会改变a,所以这永远不会结束。一旦stavros完成运行,代码将返回到parts中调用的位置,该代码完成运行并返回到brass_or_wood中调用的位置。然后它完成循环的迭代并提示bow的更多输入。如果您此时没有选择'brass''windwood',它会转到else区块并提醒您选择黄铜或木管乐器(“风木”)。使用break打破循环:

while 1:
    bow=raw_input(prompt)
    bow=bow.lower()
    if bow=="brass":
        print "That's right!"
        parts()
        break
    elif bow=="windwood":
        dead("Go play some recorder!")
        break
    else:
        print "Choose Brass or Windwood!"

您也应该为程序中的任何其他类似循环执行此操作。

第二

bb_roomtry,您拨打flats_room,在其中拨打brass_or_wood,在其中拨打parts,在其中拨打stavros 1}}。输入一个非整数,您将一直追溯到try

如果你在任何一个中获得ValueError - 基本上,在你的程序中的任何地方,因为几乎立即调用bb_room - 异常将向上传递,寻找处理程序。它会在bb_room的{​​{1}}中找到该处理程序并生成您看到的结果。