2014-10-09 79 views
-1

我想在此程序中使用while循環而不是for循環,但我不知道如何以及從何處開始?任何幫助將被讚賞!Python中的循環

def password(): 
    guessCount = 0 
    password = input("Anna please enter a password here\n:") 
    guesser = input("Guesser please enter your name here\n:") 
    print("Welcome",guesser,"to the Password Guessing Game.\nYou have 8 attempts to guess my password.\nGood Luck!") 
    for count in range (8): 
     passwordGuess = input("Guess the password\n:") 
     if passwordGuess == password: 
      guessCount = guessCount + 1 
      print("Well Done!") 
      print("It took you",guessCount,"attempt(s) to guess the password!") 
      break 
     else: 
      guessCount = guessCount + 1 
      print("Try again") 
+2

歡迎來到Stack Overflow :)這是什麼語言?請爲該語言添加標籤。另外,你爲什麼要在這裏使用'while'循環? – 2014-10-09 17:46:05

+2

如果你搜索「用戶輸入while循環」,你會發現一般的模式。你甚至可以用你正在使用的語言找到它。 – 2014-10-09 17:48:45

+0

我不知道你爲什麼想要這樣做,但是你可以直接用while來替換單詞,並在循環前有一個「count = 0」,它將全部工作。 – Spade 2014-10-09 18:05:55

回答

0

如果你想做類似於for循環的東西,你可以寫這樣的東西。

x = 0 
while x in range(8): #alternately while x<8: 
    ... 
    x +=1 

這將對範圍(8)中的x做同樣的事情。