1

私は「while」コマンドと一般的なループに苦労しています。ストップポイントがありませんが、作成方法がわかりません。原始的なストックオプションの選択をシミュレートするプログラムを作成しようとしていますが、停止点なしで最初の2つのループを循環します。どんな助けでもいただければ幸いです。

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")

ランダムモードとテストモードはプログラムの後半にありますが、どこにも停止点がないループを表示したいと思いました。2つを循環するだけです。

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」ではありません。したがって、無限ループに陥ります。これを防ぐには、ループごとに2つの異なる変数を使用します。

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 に答える