提问:如何python做一个1-100的猜数游戏,告诉我我猜的次数
网友回答:
完整的程序参考:
import random
secret = random.randint(1,100)
guess = 0
tries = 0
while int(guess) != secret and tries < 10: #最多猜10次
print("第%d次,"%(tries+1),end='');
guess = input ("输入你的数:")
if int(guess) < secret :
print ("太小!")
elif secret < int(guess):
print ("太大")
tries = tries + 1
if int(guess) == secret :
print ("你猜对了,共%d次"%(tries))
else:
print ("你10次都没猜对,数字为:",secret)