分享10个很酷、简单且有趣的 Python 项目及其源代码

在这里插入图片描述

无论我们当前的技能水平如何,学习构建 Python 项目都是提高个人技能和增强作品集的必由之路。没有什么比亲自动手进行一些实际编码和构建 Python 项目更好的了!在本文中,我们收录了 10 个有趣的 Python 项目,从适合初学者的简单 Python 项目到中级和高级 Python 项目,我们可以利用它们来挑战自己或提高 Python 编程技能。

适合初学者的 Python 项目

让我们开始为初学者提供一系列项目,如果您刚刚开始并且想要学习 Python,那么这是理想的选择。换言之,如果您渴望从事更具挑战性的事情,请阅读本文后面内容以查看更高级的项目。

1.Dice Roll Generator

Dice Roll Generator(本文译为掷骰子生成器)作为与代码初学者最相关的 Python 项目之一,该程序模拟掷一两个骰子。这也是巩固对用户定义函数、循环和条件语句的理解的好方法。这些是 Python 初学者的基本技能,并且可能是您首先要学习的一些东西,无论是从在线课程还是Python 书籍中。

作为简单的 Python 项目之一,它是一个相当简单的程序,使用 Python random 模块来复制掷骰子的随机性质。您可能还会注意到,在掷骰子后,使用 os 模块来清除屏幕。请注意,可以将最大骰子值更改为任意数字,从而允许模拟许多棋盘游戏和角色扮演游戏中常用的多面体骰子。

源代码

'''  
Dice Roll Generator  
-------------------------------------------------------------  
'''  
  
  
import random  
import os  
  
  
def num_die():  
  while True:  
      try:  
          num_dice = input('Number of dice: ')  
          valid_responses = ['1', 'one', 'two', '2']  
          if num_dice not in valid_responses:  
              raise ValueError('1 or 2 only')  
          else:  
              return num_dice  
      except ValueError as err:  
          print(err)  
  
  
def roll_dice():  
   min_val = 1  
   max_val = 6  
   roll_again = 'y'  
  
   while roll_again.lower() == 'yes' or roll_again.lower() == 'y':  
       os.system('cls' if os.name == 'nt' else 'clear')  
       amount = num_die()  
  
       if amount == '2' or amount == 'two':  
           print('Rolling the dice...')  
           dice_1 = random.randint(min_val, max_val)  
           dice_2 = random.randint(min_val, max_val)  
  
           print('The values are:')  
           print('Dice One: ', dice_1)  
           print('Dice Two: ', dice_2)  
           print('Total: ', dice_1 + dice_2)  
  
           roll_again = input('Roll Again? ')  
       else:  
           print('Rolling the die...')  
           dice_1 = random.randint(min_val, max_val)  
           print(f'The value is: {dice_1}')  
  
           roll_again = input('Roll Again? ')  
  
  
if __name__ == '__main__':  
   roll_dice()  

2.Hangman Game

这是一个有趣的 Python 项目想法,用于模拟猜词游戏 Hangman。在猜测方面使用了预定义的单词列表,但可以随意使用第三方词典 API 来改进这一点。

此 Python 项目使用循环、函数和字符串格式来打印 Hangman 的进度。它还允许试验标准库的 random、time 和 os 模块。具体来说,使用 random 模块来选择要猜测的单词,并使用 os 模块来清除屏幕,以及使用 time 模块的 .sleep() 函数来引入暂停以增强游戏流程。

如果您目前正在学习 Python 课程并且对这门语言仍然陌生,那么这是一个很好的中型项目,可以让您自我拓展!

源代码

'''  
Hangman Game  
-------------------------------------------------------------  
'''  
  
  
import random  
import time  
import os  
  
  
def play_again():  
  question = 'Do You want to play again? y = yes, n = no \n'  
  play_game = input(question)  
  while play_game.lower() not in ['y', 'n']:  
      play_game = input(question)  
  
  if play_game.lower() == 'y':  
      return True  
  else:  
      return False  
  
  
def hangman(word):  
  display = '_' * len(word)  
  count = 0  
  limit = 5  
  letters = list(word)  
  guessed = []  
  while count < limit:  
      guess = input(f'Hangman Word: {display} Enter your guess: \n').strip()  
      while len(guess) == 0 or len(guess) > 1:  
          print('Invalid input. Enter a single letter\n')  
          guess = input(  
              f'Hangman Word: {display} Enter your guess: \n').strip()  
  
      if guess in guessed:  
          print('Oops! You already tried that guess, try again!\n')  
          continue  
  
      if guess in letters:  
          letters.remove(guess)  
          index = word.find(guess)  
          display = display[:index] + guess + display[index + 1:]  
  
      else:  
          guessed.append(guess)  
          count += 1  
          if count == 1:  
              time.sleep(1)  
              print('   _____ \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '__|__\n')  
              print(f'Wrong guess: {limit - count} guesses remaining\n')  
  
          elif count == 2:  
              time.sleep(1)  
              print('   _____ \n'  
                    '  |     | \n'  
                    '  |     | \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '__|__\n')  
              print(f'Wrong guess: {limit - count} guesses remaining\n')  
  
          elif count == 3:  
              time.sleep(1)  
              print('   _____ \n'  
                    '  |     | \n'  
                    '  |     | \n'  
                    '  |     | \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '__|__\n')  
              print(f'Wrong guess: {limit - count} guesses remaining\n')  
  
          elif count == 4:  
              time.sleep(1)  
              print('   _____ \n'  
                    '  |     | \n'  
                    '  |     | \n'  
                    '  |     | \n'  
                    '  |     O \n'  
                    '  |      \n'  
                    '  |      \n'  
                    '__|__\n')  
              print(f'Wrong guess: {limit - count} guesses remaining\n')  
  
          elif count == 5:  
              time.sleep(1)  
              print('   _____ \n'  
                    &#
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值