python猜测次数_如何在Python中跟踪用户的猜测?尝试次数=尝试次数+1无效

我需要在一个简单的猜谜游戏中记录用户输入的猜测次数。

我尝试过使用attempts=0,然后将attempts设置为=attempts+1。即使我这样做,即使用户猜了不止一次,代码也会打印“你猜了1次”。

代码:attempts = 0;

print("Hello, welcome to the game. You will be choosing a number

between 1 and 100. You can only guess up to 10 times.")

for tries in range(tries_allowed):

print("You only get 10 tries.")

break

while attempts < 10:

guess = int(input("Please guess a number"));

attempts_used= attempts + 1;

if guess > random_number:

print("Guess is too high, try a smaller number");

elif guess < random_number:

print("Guess is too low, try a higher number");

elif guess == random_number:

attempts_used=str(attempts_used)

print("Correct- you win in", attempts_used, "guesses");

exit();

else:

if tries_allowed == 10:

print("You failed to guess in time")

my_list= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

my_list.append(attempts_used)

print(my_list)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值