我想用樹梅派平台設計 Python 程式語言,請幫我設計一猜數字遊戲,輸入1~100 之間的整數,依照輸入的答案,分別輸出 ”應該要小一點”、”大一點才對喔”以及”恭喜答對了”。
Jump to turn (8)
- User
我想用樹梅派平台設計 Python 程式語言,請幫我設計一猜數字遊戲,輸入1~100 之間的整數,依照輸入的答案,分別輸出 ”應該要小一點”、”大一點才對喔”以及”恭喜答對了”。
- Assistant
以下是使用 Python 在 Raspberry Pi 平台上設計一個猜數字遊戲的程式碼。
pythonimport random def guess_number(): secret_number = random.randint(1, 100) attempts = 0 while True: try: user_guess = int(input("請輸入1~100之間的整數:")) if user_guess < secret_number: attempts += 1 print("應該要大一點") elif user_guess > secret_number: attempts += 1 print("應該要小一點") else: attempts += 1 print(f"恭喜答對了!你總共猜了 {attempts} 次") break except ValueError: print("請輸入數字") guess_number()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.