这是一个简单的 Python 猜数字游戏,适合初学者学习编程。

import random

print('Welcome to the Number Guessing Game!')
print('I'm thinking of a number between 1 and 100.')

number = random.randint(1, 100)
guess = None
tries = 0

while guess != number:
    guess = int(input('Take a guess: '))
    tries += 1
    if guess < number:
        print('Too low.')
    elif guess > number:
        print('Too high.')
        
print(f'You got it! The number was {number}.')
print(f'It took you {tries} tries.')

游戏规则:

  1. 程序会随机生成一个 1 到 100 之间的整数。
  2. 玩家需要猜测这个数字是多少。
  3. 每次猜测后,程序会告诉玩家猜的数字是偏高还是偏低。
  4. 直到玩家猜中为止。
  5. 最后程序会告诉玩家猜中了多少次。

通过这个简单的游戏,你可以学习 Python 的基本语法,包括变量、循环、条件语句、输入输出等。快来试试吧!


原文地址: https://www.cveoy.top/t/topic/mrgk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录