gpt4 book ai didi

python - 下面脚本的 while 循环

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:24 25 4
gpt4 key购买 nike

我目前正在创建一个游戏,它会一遍又一遍地循环,直到您猜对数字为止。

我遇到的问题是正确执行循环命令。我想进行 while 循环,但对于脚本的某些部分,我可以让它工作。如果我使用“while true”循环,if 语句的打印命令会一遍又一遍地重复,但如果我使用任何符号(<、>、<=、>= 等),我似乎无法让它工作在 elif 语句上。代码可以在下面找到:

#GAME NUMBER 1: GUESS THE NUMBER
from random import randint
x = randint(1,100)
print(x) #This is just here for testing
name = str(input("Hello there, my name's Jarvis. What's your name?"))
print("Hello there ",name," good to see you!")
num = int(input("I'm thinking of a number between 1 and 100. can you guess which one it is?"))
attempt = 1
while
if num == x:
print("Good job! it took you ",attempt," tries!")
num + 1
elif num >= x:
print("Too high!")
attempt = attempt + 1
elif num <= x:
print("Too low!")
attempt = attempt + 1
else:
print("ERROR MESSAGE!")

感谢任何帮助。

最佳答案

您可以在 while 中使用 bool 值:

from random import randint
x = randint(1,100)
print(x) #This is just here for testing
name = str(input("Hello there, my name's Jarvis. What's your name?"))
print("Hello there ",name," good to see you!")
attempt = 1
not_found = True
while not_found:
num = int(input("I'm thinking of a number between 1 and 100. can you guess which one it is?"))
if num == x:
print("Good job! it took you ",attempt," tries!")
not_found = False
elif num > x: #Don't need the equals
print("Too high!")
elif num < x: #Don't need the equals
print("Too low!")
else:
print("ERROR MESSAGE!")
attempt = attempt + 1

关于python - 下面脚本的 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33834933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com