import random secret = random.randint(1,10) temp = input("caiyixia:") guess = int(temp) times = 1 while (guess != secret) and (times < 3): if guess > secret: print("dale") else: print("xiaole") temp = input ( ) guess = input(temp) times = times + 1 if (times <= 3) and (guess == secret): print("great") else: print("buxing")
如果是猜中了程序没有问题,但是如果没有猜中,就会报错
import random computer=random.randint(1,100) while True: number=int(input("请输入100以内的整数:")) if(number>computer): print("大了") elif(number<computer): print("小了") else: print("恭喜你赢了") break
这一段代码通过break实现,就没有问题