1. 程式人生 > >猜數遊戲:隨機生成一個數,限制次數

猜數遊戲:隨機生成一個數,限制次數

import random
times = 3
secret = random.randint(1,10)
guess = 0
print("猜猜數字:")
while (guess != secret) and (times > 0):
    temp = input()
    while not temp.isdigit():
        temp = input("請重新輸入一個整數:")
    guess = int(temp)
    times = times - 1 # 每輸入一次,可用機會就-1
if guess == secret:
        print("獎勵!
") else: if guess > secret: print("大了大了~~~") else: print("小了,小了~~~") if times > 0: print("再試一次吧:") else: print("機會用光") print("遊戲結束,Game over")