18-五次机会猜数游戏

#核心是break的效果,当执行break的时候,while循环的else不再执行;
import random
number = random.randint(1,10)
counter = 0
while counter < 5:
    counter += 1
    answer = int(input('guess(1-10):'))
    if answer > number:
        print('猜大了')
    elif answer < number:
        print('猜小了')
    else:
        print('恭喜你,猜对了')
        break
else:
    print('正确答案是:' % number)

你可能感兴趣的:(18-五次机会猜数游戏)