1

我在一般的“while”命令和循环中苦苦挣扎。我错过了一个停止点,但我不知道如何创建一个。我正在尝试创建一个程序来模拟原始股票期权选择,但它只是循环通过前两个循环而没有停止点。任何帮助,将不胜感激。

selection = " "
while selection not in ("r","R","t","T"):
print("This program can operate in random mode (fluctuations will randomly occur) or it      can operate in test mode (fluctuations are fixed).")
print("(r)andom mode")
print("(t)est mode")
selection = input("Mode of operation:")
if selection not in ("r","R","t","T"):
    print("\nPlease select 'r' or 't' for mode selection\n")
elif (selection == "r"):
    print("Random mode enabled")
    n = random.randrange(1,101)
    selection = " "
    while selection not in ("1","2","3"):
        print("\tInvestment options:")
        print("(1)Fly-By-Night Investments (FBN): high risk, high potential returns")
        print("(2)Blue Chips INC. (BCI): moderate risk, good potential yearly returns")
        print("(3)Slow-And-Steady-Corp. (SNS), mature industry stock: no risk but low returns")
        selection = input("Please enter your investment option 1, 2, or 3:")
        if selection not in ("1","2","3"):
            print("\nPlease enter an investment option from the menu using 1, 2, or 3 as valid selections:\n")

随机和测试模式在程序的后面出现,但我想展示我在任何地方都没有停止点的循环。它只是在两者之间循环。

4

1 回答 1

0

你被困在一个无限循环中,因为你重新定义了selection.

selection = ''
while selection.lower() not in ("r","t"):
    print("This program can operate in random mode (fluctuations will randomly occur) or it can operate in test mode (fluctuations are fixed).")
    print("(r)andom mode")
    print("(t)est mode")
    selection = input("Mode of operation:")
    if selection.lower() not in ("r","t"):
         print("\nPlease select 'r' or 't' for mode selection\n")
    elif (selection.lower() == "r"):
        print("Random mode enabled")
        n = random.randrange(1,101)
        selection = '' # redefined here
        while selection not in ("1","2","3"): 
            print("\tInvestment options:")
            print("(1)Fly-By-Night Investments (FBN): high risk, high potential returns")
            print("(2)Blue Chips INC. (BCI): moderate risk, good potential yearly returns")
            print("(3)Slow-And-Steady-Corp. (SNS), mature industry stock: no risk but low returns")
            selection = input("Please enter your investment option 1, 2, or 3:")
            if selection not in ("1","2","3"):
                print("\nPlease enter an investment option from the menu using 1, 2, or 3 as valid selections:\n")

请注意您如何selection再次使用。一旦它到达内部while循环,selection就会变成 1、2 或 3(或其他任何值),但很可能不是 'r' 或 't'。因此,它陷入了无限循环。为了防止这种情况,每个循环使用两个不同的变量。

selection = ''
while selection.lower() not in ("r","t"):
    print("This program can operate in random mode (fluctuations will randomly occur) or it can operate in test mode (fluctuations are fixed).")
    print("(r)andom mode")
    print("(t)est mode")
    selection = input("Mode of operation:")
    if selection.lower() not in ("r","t"):
         print("\nPlease select 'r' or 't' for mode selection\n")
    elif (selection.lower() == "r"):
        print("Random mode enabled")
        n = random.randrange(1,101)
        investmentChoice = ''
        while investmentChoice not in ("1","2","3"): 
            print("\tInvestment options:")
            print("(1)Fly-By-Night Investments (FBN): high risk, high potential returns")
            print("(2)Blue Chips INC. (BCI): moderate risk, good potential yearly returns")
            print("(3)Slow-And-Steady-Corp. (SNS), mature industry stock: no risk but low returns")
            investmentChoice = input("Please enter your investment option 1, 2, or 3:")
            if investmentChoice not in ("1","2","3"):
                print("\nPlease enter an investment option from the menu using 1, 2, or 3 as valid selections:\n")
于 2013-03-01T04:05:31.390 回答