2013-03-01 46 views
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") 

隨機和測試模式後來在節目中來了,但我想證明,我沒有一個停車點的任何位置的循環。它只是循環兩個。

+2

在ifs中使用「break」 – sqram 2013-03-01 03:55:39

回答

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