2014-10-09 77 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做同样的事情。