Python中的数字游戏 来自随机导入randint hp=4 i=0 随机数=randint(1,25) 当我
我想知道为什么它不起作用。所有的台词都对吗?它是循环,有解决方案吗?我不确定您的缩进,因为不清楚它在您的实际程序中是什么样子的,但是:Python中的数字游戏 来自随机导入randint hp=4 i=0 随机数=randint(1,25) 当我,python,python-3.x,Python,Python 3.x,我想知道为什么它不起作用。所有的台词都对吗?它是循环,有解决方案吗?我不确定您的缩进,因为不清楚它在您的实际程序中是什么样子的,但是: from random import randint hp=4 i = 0 random_number=randint(1,25) while i < 5: Num=int(input("Please Enter Number")) if int(Num)==random_number: print("you win") if random_numb
from random import randint
hp=4
i = 0
random_number=randint(1,25)
while i < 5:
Num=int(input("Please Enter Number"))
if int(Num)==random_number:
print("you win")
if random_number!=Num:
print("Incorrect")
hp=hp-1
print("your hp is {}".format(hp))
if int(Num) == str:
print("Please Enter Number")
if hp==0 :
print("You Lose ")
i += 1
应置于try catch中,以查看输入是否可转换为int(即,它只接受数字输入)
对游戏进行一些更改
输出
您希望此行做什么:
如果int(Num)==str:
如果Num==str打印错误,因为Num必须只有numbers这将不起作用,但看起来您的缩进是错误的
Num=int(input("Please Enter Number"))
try:
Num = int(input("Please insert a number"))
except ValueError:
Print("Please enter a number instead")
i = i - 1
from random import randint
from time import sleep
chances = 4
elenco = []
random_number = randint(1, 25)
while chances > 0:
num = input("Please Enter Number: ")
if num == str(random_number):
print("you win")
else:
elenco.append(num)
print("\t These numbers are incorrect: " + "-".join(elenco))
chances = chances - 1
print("\tYou still have {} chances".format(chances) + "\n")
sleep(1)
print("\nGame over ")
print("The right answer was {}".format(random_number))