1-10内随机产生自然数进行猜数:
import random
n =random.randint(0,10)
while True :
user_guess=int (input ("Input your guess:"))
if user_guess>n:
print("Try smaller...")
elif user_guess<n:
print("Try bigger...")
else :
print("Bingo,you got it!")
break
指定所猜数字为100进行猜数:
n=100
i=0
while True :
user_guess=input ()
if user_guess.isdigit():
user_guess=int(user_guess)
if user_guess>n:
print("larger than expected")
i+=1
elif user_guess<n:
print("less than expected")
i+=1
else :
print("you have tried %d times,you win"%(i+1))
break
else:
print("input error")
i+=1
|