Python中的while循环

2024-05-26 20:46:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在这个程序中使用while循环,而不是for循环,但是我不知道如何以及从哪里开始?任何帮助都会很感激的!在

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

Tags: theto程序forinputherepasswordprint
1条回答
网友
1楼 · 发布于 2024-05-26 20:46:37

如果你想做类似于for循环的事情,你可以写这样的东西。在

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

这将对范围(8)中的x执行相同的操作。在

相关问题 更多 >

    热门问题