Python 随机猜测游戏
我正在学习如何专门使用Python编程,但我不知道如何让代码再次播放。如果你能帮忙,那太好了Python 随机猜测游戏,python,Python,我正在学习如何专门使用Python编程,但我不知道如何让代码再次播放。如果你能帮忙,那太好了 #This program is a Random Number Guessing game import random rand_num = random.randint(1, 100) count = 1 guess = int(input("Guess a number between 1 and 100.")) while guess != rand_num: count = cou
#This program is a Random Number Guessing game
import random
rand_num = random.randint(1, 100)
count = 1
guess = int(input("Guess a number between 1 and 100."))
while guess != rand_num:
count = count + 1
if guess < rand_num:
print ('Too low, try again.')
guess = int(input("What number is it?"))
elif guess > rand_num:
print ('Too high, try again.')
guess = int(input("What number is it?"))
if count > 1:
print('You Won! It took you', count, 'times.')
else:
print('You won! It took you', count, 'try.')
在代码周围添加一个while循环
#This program is a Random Number Guessing game
import random
play = True
while play:
rand_num = random.randint(1, 10)
count = 1
guess = int(input("Guess a number between 1 and 10."))
while guess != rand_num:
count = count + 1
if guess < rand_num:
print ('Too low, try again.')
guess = int(input("What number is it?"))
elif guess > rand_num:
print ('Too high, try again.')
guess = int(input("What number is it?"))
if count > 1:
print('You Won! It took you', count, 'times.')
else:
print('You won! It took you', count, 'try.')
s = input("Would you like to play again? ")
play = s.lower() in ('y', 'yes', 'ok', 'sure')
您可以这样做:
import random
while True:
rand_num = random.randint(1, 100)
count = 1
guess = int(input("Guess a number between 1 and 100."))
while guess != rand_num:
count = count + 1
if guess < rand_num:
print ('Too low, try again.')
guess = int(input("What number is it?"))
elif guess > rand_num:
print ('Too high, try again.')
guess = int(input("What number is it?"))
if count > 1:
print('You Won! It took you', count, 'times.')
else:
print('You won! It took you', count, 'try.')
print("You want to play more !!! Y or anything else")
decision = str(input())
if(decision=="Y"):
continue
else:
break
使用无限循环,条件是要求用户播放更多..Y或N,如果可以,请键入代码,以便我可以获得视觉效果,好吗?谢谢你抽出时间,谢谢!我最终找到了答案,但这很有帮助D