程序随机内置一个位于一定范围内的数字作为猜测的结果,由用户猜测此数字。
用户每猜测一次,由系统提示猜测结果:太大了、太小了或者猜对了,直到用户猜对结果或者猜测次数用完导致失败。
import random
num = random.randint(0,99)
count =0
while count<3:
guess = int(input("please input your guess int:"))
if guess == num:
print(f"bingo! {guess} is the right number!")
break
elif guess > num:
print(f"{guess} is too big, please try smaller.")
else:
print(f"{guess} is too small, please try bigger.")
if count == 2:
print(f"都没猜对,继续努力")
count+=1
设定一个理想数字比如:66, 让用户三次机会猜数字,如果比66大,则显示猜测的结果大了; 如果比66小,则显示猜测的结果小了; 只有等于66,显示猜测结果正确,退出循环。
最多三次都没有猜测正确,退出循环,并显示‘都没猜对,继续努力’
num = 66
count =0
while count<3:
guess = int(input("please input your guess int:"))
if guess == num:
print(f"bingo! {guess} is the right number!")
break
elif guess > num:
print(f"{guess} is too big, please try smaller.")
else:
print(f"{guess} is too small, please try bigger.")
if count == 2:
print(f"都没猜对,继续努力")
count+=1