结束Python脚本

时间:2012-03-21 12:32:08

标签: python python-3.x

在Python 3.2中,我正在编写一个基本菜单程序,当输入退出选项时,该功能没有结束。 当选择quit时,它会结束脚本其余部分所在的循环,并且应该终止脚本,但不管是出于何种原因? 我错过了一个杀死脚本的'end'函数,还是新的Python Shell只是错误? 很确定这在Python 2.7中不是必需的。

import random
choice = str(input("\nMenu:\n(I)nstructions\n(C)alculate blah\n(Q)uit\n>>>"))
while choice != "q" or choice != "Q":
    while choice != "i" and choice != "I" and choice != "c" and choice != "C" and choice != "q" and choice != "Q":
        print("Invalid menu choice.")
        choice = str(input("\nMenu:\n(I)nstructions\n(C)alculate blah\n(Q)uit\n>>>"))
    if choice == "i" or choice == "I":
        print("blahblah.")
        choice = str(input("\nMenu:\n(I)nstructions\n(C)alculate blah\n(Q)uit\n>>>"))   
    if choice == "c" or choice == "C":
        x = int(input("Please enter the number of x: "))
        while x < 0:
            x = int(input("Please enter the number of x: "))
        y = int(input("Please enter the number of y: "))
        while y < 0:
            y = int(input("Please enter the number of y: "))
        z = str(input("blah (B) or (P) z?: "))
        while z != "b" and z != "p" and z != "B" and z != "P":
            z = str(input("blah (B) or (P) z?: "))
        if z == "b" or z == "B":
            total = x*10 + y*6 + 0
            print("blah $", total, " blah ", x, " x and ", y, " y. blah!")
        #function that outputs the cost of premium z
        if z == "p" or z == "P":
            luck = random.randrange(1, 11, 1)
            if luck == 10:
                total = x*10 + y*6
                print("\nblah$", total, " blah z for ", x, " x and ", y, " y. blah!")
            #below is the normal function, for when the customer is not a lucky winner
            if luck != 10:
                total = x*12.50 + y*7.50
                print("blah $", total, " blah ", x, " x and ", y, " y. blah!")
        choice = str(input("\nMenu:\n(I)nstructions\n(C)alculate\n(Q)uit\n>>>"))

4 个答案:

答案 0 :(得分:5)

你的情况错了:

while choice != "q" or choice != "Q":    # this should be "and"!

始终返回True,创建无限循环。

此外,你在这里有一个令人费解的逻辑。这可以简化很多:

import random
while True:
    choice = str(input("\nMenu:\n(I)nstructions\n(C)alculate blah\n(Q)uit\n>>>")).lower()
    if choice == "i":
        print("blahblah.")
        continue
    elif choice == "q":
        break
    elif choice == "c":
        while True:
            x = int(input("Please enter the number of x: "))
            if x >= 0: break
        while True:
            y = int(input("Please enter the number of y: "))
            if y >= 0: break
        while True:
            z = str(input("blah (B) or (P) z?: ")).lower()
            if z in "bp": break
        if z == "b":
            total = x*10 + y*6 + 0
            print("blah $", total, " blah ", x, " x and ", y, " y. blah!")
        #function that outputs the cost of premium z
        else:  # z must be "p"
            luck = random.randrange(1, 11, 1)
            if luck == 10:
                total = x*10 + y*6
                print("\nblah$", total, " blah z for ", x, " x and ", y, " y. blah!")
            #below is the normal function, for when the customer is not a lucky winner
            if luck != 10:
                total = x*12.50 + y*7.50
                print("blah $", total, " blah ", x, " x and ", y, " y. blah!")
    else:
        print("Invalid menu choice.")
        continue

答案 1 :(得分:1)

在Python中执行退出操作的一种方法是抛出自定义异常并显式捕获该异常。如果我错了,请纠正我,AFAIK这并不会对你的python程序造成太大的影响。它可以像我在下面显示的那样简单:

...
class QuitException(Exception);
...
def MyMenuProgram():
    ...
    ...
    ...

if __name__ == '__main__':
    try:
        MyMenuProgram()
    catch QuitException:
        pass
    catch Exception, e:
        raise

答案 2 :(得分:0)

编辑:已编辑 - input()确实是在Py3k中获取用户输入的方式。 Python 2.7和Python 3.2之间的另一个光荣差异。

答案 3 :(得分:0)

当@Tim回答时,你的循环条件是错误的。

while choice != "q" or choice != "Q":

让我们从逻辑上看一下这个:

  1. 如果选择==“q”,则选择!=“Q”,循环条件评估为false or true,即true
  2. 如果选择==“Q”,则选择!=“q”,循环条件评估为true or false,即true
  3. 如果选择!=“q”或“Q”,循环条件评估为true or true,即true
  4. 您需要将此循环条件更改为:

    while choice != "q" and choice != "Q":
    

    while choice.lower() != "q":
    
相关问题