[英]How do u add lives and a counter to display the lives
I want to add lives to and a counter displaying how many lives I have left 我想增加生命,并建立一个计数器,显示我还剩下多少生命
I tried adding a counter using range but it prints all the no like 1, 2, 3 I just need it to display 1 number that represents lives u have left 我尝试使用范围添加一个计数器,但它会打印出所有不像1、2、3的数字,我只需要它显示1个代表您已经离开的生活的数字
num = random.randint(1,10);
print(num)
guess = int(input('Guess a number: '))
if(guess == num):
print("correct")
else:
print("incorrect")
Try a for loop: 尝试for循环:
import random
num = random.randint(1,10)
for i in range(3):
guess = int(input('Guess a number: '))
if(guess == num):
print("correct")
break
else:
print("incorrect")
print(2 - i, 'tries left')
I would use a while loop for this. 我会为此使用while循环。 Each guess a counter would be decreased by one.
每次猜测计数器将减少一。 The loop would run while lives > 0.
循环将在生命> 0时运行。
num = random.randint(1,10);
print(num)
lives = 3
while lives > 0:
guess = int(input('Guess a number: '))
if(guess == num):
print("correct")
break
else:
print("incorrect")
print("%s lives left." % lives-1)
lives -= 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.