【问题标题】:Trouble closing loops or creating a prompt to progress无法关闭循环或创建进度提示
【发布时间】:2013-03-01 03:52:06
【问题描述】:

我在一般的“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")

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

【问题讨论】:

  • 在 ifs 中使用“break”

标签: python loops if-statement while-loop prompt


【解决方案1】:

因为你重新定义了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")

【讨论】:

    猜你喜欢
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多