程式碼如下:
import random
num_auto = random.randint(1,101)
min_num = 1
max_num = 100
N = 1
print("-------猜數字遊戲-------\n請輸入整數,你只有五次機會!\n----加油噢!----")
while N <= 5:
num_user = input('請輸入你猜的數:')
if num_user == 'exit':
break
elif num_user != num_auto and num_user != 'exit':
num_user = int(num_user)
if num_user > num_auto:
print('猜錯了喲!太大了')
N +=1
elif num_user < num_auto:
print('猜錯了喲!太小了')
N +=1
else:
print('你對了!' + '猜了' + str(N) + '次')
break
else:
print('Game Over!\n已超過5次')