结束Python脚本

Epi*_*mic -1 python python-3.x

在Python 3.2中,我正在编写一个基本菜单程序,当输入退出选项时,该功能没有结束.当选择退出时,它会结束脚本的其余部分所在的循环,并且应该终止脚本,但它不是,无论出于何种原因?我错过了一个杀死脚本的'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>>>"))
Run Code Online (Sandbox Code Playgroud)

Tim*_*ker 5

你的病情错了:

while choice != "q" or choice != "Q":    # this should be "and"!
Run Code Online (Sandbox Code Playgroud)

总是返回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
Run Code Online (Sandbox Code Playgroud)