python的猜数游戏中怎么询问是否愿意继续猜数_Python数字猜谜游戏需要帮助避免在“猜数”中重复计数...

跟踪猜测的数字,只有在用户还没有猜到我们猜测的数字集中的数字时才会增加:import random

print("Guess a number between 1-100")

the_number = random.randint(1, 100)

tries = 0

# store all the user guesses

guessed = set()

while True:

guess = int(input("Guess a number: "))

# if the guess is in our guesses set, the user has guessed before

if guess in guessed:

print("You already guessed that number!")

continue

# will only increment for unique guessed

tries += 1

if guess == the_number:

print("You win! The number was", the_number)

print("And it only took you", tries, "tries!\n")

break

elif guess > the_number:

print("Lower")

# if it's not == or >, it has to be <

else:

print("Higher")

# add guess each time

guessed.add(guess)

对于你的排序,你也有一些逻辑,比如在循环之外进行猜测,这可能意味着如果用户第一次猜到了,你就永远不会进入循环。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值