2016-11-17 47 views
0

我遇到了這個數據驗證程序的問題,我有一個任務。由於某些原因,我在這個while循環中遇到了麻煩,它繼續無限期地運行。其他while循環在我的代碼運行正常的,但在這裏這個功能不...while循環以及如何在函數中停止它

def menu():                        
    pick = None 
    while pick != "q" or pick != "Q": 
     print """ 
    \n 

    Welcome to my password validation program. 

    1 - New Account 
    2 - Login 
    q - Quit 

    """ 
    pick = raw_input("What do you want to do?: ") 
    # Quit 
    if pick == "q": 
     print "\t Thanks for coming Good-bye." 
     raw_input("\nPress any key to exit") 
    # New Account 
    elif pick == "1": 
     main()                      
    # Login 
    elif pick == "2": 
     if ps == 100: 
      login()                     
     else: 
      print "Sorry no password has been set. please create a new account." 
    else: 
     print "Sorry, but", pick, "isn't a valid choice." 

menu() 

有大約走錯了路的代碼,無論是你的答案是正確的。 @jonrsharpe @tdelaney

def menu(): 
     print \ 
     """ 
     \n 

     Welcome to my password validation program. 

     1 - New Account 
     2 - Login 
     q - Quit 

     """ 

     pick = None 
     pick = raw_input("What do you want to do?: ") 
     # Quit 
     if pick == "q": 
      print "\t Thanks for coming Good-bye." 
      raw_input("\nPress any key to exit") 
     # New Account 
     elif pick == "1": 
      main()                      
     # Login 
     elif pick == "2": 
      if ps == 100: 
       login()                     
      else: 
       print "Sorry no password has been set. please create a new account." 
     else: 
      print "Sorry, but", pick,"isn't a valid choice." 

    menu() 
+1

你意識到只有'print'行在while循環中? – jonrsharpe

+0

那麼這是如何阻止無限循環的呢?我知道打印行會打印出下面的文字,但它與無限循環有什麼關係 – Will

+1

***只有* print'行**其中**不會改變'pick'的值* *。你的函數的其餘部分只在循環結束後運行*,它不能*。 – jonrsharpe

回答

0

我覺得這裏邊可以發佈您的代碼......如果不是你print """線要保持印刷只要挑不等於q或你可能有一些複製/粘貼錯誤問:

1

這裏的問題是你的壓痕,
這樣做:

def menu():                        
    pick = None 
    while pick != "q" or pick != "Q": 
     print """ 
     \n 

     Welcome to my password validation program. 

     1 - New Account 
     2 - Login 
     q - Quit 

     """ 
     pick = raw_input("What do you want to do?: ") 
     # Quit 
     if pick == "q": 
      print "\t Thanks for coming Good-bye." 
      raw_input("\nPress any key to exit") 
     # New Account 
     elif pick == "1": 
      main() 
     # Login 
     elif pick == "2": 
      if ps == 100: 
       login() 
      else: 
       print "Sorry no password has been set. please create a new account." 
     else: 
      print "Sorry, but", pick, "isn't a valid choice." 

menu() 

pick PARAMET的賦值呃必須在while循環範圍內。 另一件事是在mainlogin功能沒有定義,所以請確保它在您的本地範圍或導入到範圍。