2022年适合初学者的十大有趣而简单的Python项目

883 阅读14分钟

今天的Python是使用最广泛的编程语言之一,而且这种趋势似乎会一直延续到2022年及以后。因此,如果你刚开始学习Python,在一些现有的Python项目想法上下功夫是你能做的最好的事情。实践的方法是有助于增强学术知识,并通过实际经验为现实世界的工作场所做准备。在这篇文章中,我们将看看几个令人愉快和简单的Python项目,初学者可以使用Windows下最好的Python编辑器来练习他们的编程技能。

有哪些适合初学者的有趣的Python项目?

基于项目的学习增强了学生的学习能力。当涉及到软件开发的工作时,在他们的项目上工作是对雄心勃勃的开发者的一个要求。开发真实世界的项目是磨练你的能力并将你的学术知识转化为实践经验的最好方法。

如何用Python创建一个数字猜测游戏?

这个项目是一个有趣的游戏,用户必须在得到提示后预测在给定范围内生成的随机数。每次用户的估计不正确时,都会提示用户有进一步的建议来帮助他们(以减少分数为代价)。

该应用程序还具有确定用户是否输入数字和两个数字之间的差异的功能。

对于一个数字猜测项目,我们需要实现以下内容。

  1. 接受用户输入的一个随机数的上限和下限。
  2. 要求用户在该范围内猜一个随机数。
    1. 如果猜测结果高于该数字,则告诉用户该数字更高。
    2. 如果猜测低于该数字,则告诉用户该数字较低。
    3. 如果猜测与数字相同,则结束游戏。
  3. 如果猜测的数字超过一定的数量就结束,否则就循环到2。
import random
import math
# Taking Inputs
lower = int(input("Enter Lower bound:- "))

# Taking Inputs
upper = int(input("Enter Upper bound:- "))

# generating random number between
# the lower and upper
x = random.randint(lower, upper)
print("ntYou've only ",
	round(math.log(upper - lower + 1, 2)),
	" chances to guess the integer!n")

# Initializing the number of guesses.
count = 0

# for calculation of minimum number of
# guesses depends upon range
while count < math.log(upper - lower + 1, 2):
	count += 1

	# taking guessing number as input
	guess = int(input("Guess the number:- "))

	# Condition testing
	if x == guess:
            if count == 1:
                  print("Congratulations you did it in a single try!")                      
            else:
		      print("Congratulations you did it in ",
			      count, " tries")
		# Once guessed, loop will break
		break
	elif x > guess:
		print("You guessed too small!")
	elif x < guess:
		print("You Guessed too high!")

# If Guessing is more than required guesses,
# shows this output.
if count >= math.log(upper - lower + 1, 2):
	print("nThe number is %d" % x)
	print("tBetter Luck Next time!")

如何用Python制作一个剪刀石头布项目?

这个程序使用各种Python函数来创建游戏玩法,所以它是熟悉这个关键想法的绝佳机会。

在做动作之前,程序等待用户采取行动。可以用代表石头、纸或剪刀的字符串或字母作为输入。在对输入的字符串进行评估后,结果函数决定赢家,而记分员函数则更新该轮的分数。

为了实现一个数字猜测游戏,我们需要一个算法。我们可以通过以下方式实现该项目。

  1. 输入一个用户动作(石头、纸、或剪刀)。
  2. 生成一个随机动作,即石头、纸或剪刀。
  3. 使用石头、纸和剪刀的规则来决定哪个玩家是赢家。
  4. 输出结果。

使用上述算法,下面是代码的样子。

import random

# Inputting user action
user_action = input("Enter a choice (rock, paper, scissors): ")

# Generating Computer action using the possible choices.
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"nYou chose {user_action}, computer chose {computer_action}.n")


# Using the general rules of Rock, Paper, Scissor to determine the 
# winner.
if user_action == computer_action:
    print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
    if computer_action == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose.")
elif user_action == "paper":
    if computer_action == "rock":
        print("Paper covers rock! You win!")
    else:
        print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
    if computer_action == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose.")

骰子卷生成器在Python中是如何工作的?

骰子滚动生成器生成随机数,并使用随机函数模拟滚动骰子。许多棋盘和角色扮演游戏中使用的多面体骰子可以通过将最大值改为任何数字来进行复制。

你可以用这个掷骰子发生器来玩鲁道夫、蛇和梯子等游戏!

这个项目实现起来很简单,但我们可以更进一步,继续滚动骰子,直到用户说 "不"。以下是该算法的样子

  1. 输入用户的最小和最大范围(这些是骰子将随机采取的数值。
  2. 输入用户在每次尝试时想要的掷骰子次数。
  3. 生成并输出骰子掷出的数值。
  4. 问用户是否要继续。
    1. 如果继续,循环回到3。
    2. 否则结束程序

下面是这个简单程序的样子。

import random

# Enter the mini and max integer limits of the dice rolls below
# 1 is added to the maximum value as it is not included in the range

min = int(input("Enter minimum value"))
max = int(input("Enter maximum value")) + 1

# Store the user's decision
roll = "yes"

while roll == "yes":

    print("Dice rolling...")
    print("Dice values are :")

    # Printing the randomly generated variable of the dice 1
    print(random.randint(min, max))

    # Prompt user enters yes to continue 
    # Any other input ends the program
    roll = input("Do you want to roll the dice again?")

用Python做一个计算器容易吗?

是的,它属于比较简单的项目。你可以通过这个项目学习如何创建一个图形用户界面,这也是了解像Tkinter这样的库的绝佳途径。在这个库的帮助下,你可以设计执行各种任务的按钮,并在屏幕上显示结果。

对于我们的简单计算器,我们将实现基本的算术功能:加法、减法、乘法和除法。这个简单的基于终端的计算器将在终端中使用简单的Python输入。其算法将是这样的。

  1. 询问用户是否要进行加、减、乘、除运算。
  2. 要求用户提供两个数字,对其进行算术运算。
  3. 将生成的新数字输出到终端屏幕上。

下面是我们的程序的样子。

# Function for addition of two numbers
def add(x, y):
    return x + y

# Function for subtraction of two numbers
def subtract(x, y):
    return x - y

# Function for multiplication of two numbers
def multiply(x, y):
    return x * y

# Function for division of two numbers
def divide(x, y):
    return x / y


print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

while True:
    # Take a choice input from the user
    choice = input("Enter choice of what to do(1/2/3/4): ")

    # Check the choice against the listed operations
    if choice in ('1', '2', '3', '4'):
        number1 = float(input("Enter first number: "))
        number2 = float(input("Enter second number: "))

        if choice == '1':
            print(number1, "+", number2, "=", add(number1, number2))

        elif choice == '2':
            print(number1, "-", number2, "=", subtract(number1, number2))

        elif choice == '3':
            print(number1, "*", number2, "=", multiply(number1, number2))

        elif choice == '4':
            print(number1, "/", number2, "=", divide(number1, number2))
        
        # Asking if user wants to do another calculation
        next = input("Do you want to do another calculation? (yes/no): ")
        if next == "no":
          break
    
    else:
        print("Invalid Input")

请注意,我们已经为各个算术函数创建了几个辅助函数。你可以在打印语句中做这些,但我们选择这样做是为了提高代码的可读性,使教程更容易理解。

如何在Python中创建一个倒数计时器?

一个倒计时器就像它的名字一样。它从输入的秒数开始倒数,直到显示一条信息。它使用时间模块,这一点很重要,而且是一个相对简单的模块。

为了制作一个简单的倒计时器,我们将使用 Python 的时间模块。时间模块的睡眠方法允许我们控制我们的循环的睡眠时间。我们可以构建一个算法,如下所示。

  1. 输入用户的倒计时时间。
  2. 每秒钟从输入的时间开始递减,直到时间达到零。在每次递减时输出。

下面是我们的最终代码的样子。

import time

def countdown(t):
    # Looping through the time until time reaches 0
    while t != 0:
        # Displaying the minutes and seconds remaining
        min, sec = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(min, sec)
        print(timer)
        print(“n”)
        # Sleeping for 1 second before decrementing the time
        time.sleep(1)
        t -= 1

    # Printing that the countdown has come to an end
    print('Countdown over!')

# Prompt user to enter the countdown time
t = input("Enter the countdown time in seconds: ")


# Calling the function to start counting down the time
countdown(int(t))

什么是Mad Libs generator Python项目?

鉴于它使用了字符串、变量和连接,这个Python入门项目对于新手来说是一个不错的开始。Mad Libs Generator 处理的输入数据可以是一个形容词、一个代词或一个动词。在接收到输入后,该程序组织事实,创造一个故事。如果你是编码新手,你应该试试这个很棒的Python项目。

Mad Libs是一个有趣而简单的项目,即使是新手也可以做。它本质上是一个硬编码程序,你可以设计你自己的故事,所以你可以设计你自己的故事并以代码的形式实现。然而,每个Mad Libs项目的一些基本功能都是相同的。

  1. 设计一个你想讲的故事,并留有空白处。
  2. 要求用户根据定制的问题列表输入答案。
  3. 将用户输入的内容添加到你的故事和输出中。

这里有一个故事的示例代码。

loop = 1

while (loop < 10):

    # List of all questions that the program asks the user
    noun = input("Type a noun: ")
    p_noun = input("Type a plural noun: ")
    noun2 = input("Type another noun: ")
    place = input("Name a place: ")
    adjective = input("Type an adjective: ")
    noun3 = input("Type a third noun: ")

    # Display story created based on the users input
    print ("Be kind to your", noun, "-handed", p_noun)
    print ("For a dog may be somebody's", noun2,",")
    print ("Be kind to your", p_noun, "in", place)
    print ("Where the mood is always", adjective,".n")
    print ("You may think whether this is the",noun3,",")
    print ("Well it is!")

    # Loop back to "loop = 1"
    loop = loop + 1

如何在Python中创建一个刽子手游戏?

刽子手更像是一个猜字游戏。在创建这个项目时,你必须采用以下基本概念:变量、随机、整数、字符串、char、输入和输出,以及布尔值。用户必须在游戏中提交字母猜测,每个用户都有固定的猜测次数(需要一个计数器变量来限制猜测次数)。Hangman是最强烈建议初学者学习Python的项目之一。

用户可以从一个预先安排好的单词列表中选择术语,你可以生成。此外,你必须实现特定的例程来验证用户是否提交了一个字母,或者输入的字母是否在隐藏的单词中,以确定用户是否真的输入了一个字母,并打印相应的结果(字母)。这里有一个GitHub存储库的样本,可以帮助你开始使用。

在Python中创建一个电子邮件切片器容易吗?

其中一个有用的Python项目,在未来会非常有用。所有电子邮件地址的第一部分,即@符号之前的部分,包含公司的别名、用户、小组或部门。@"是电子邮件地址中的一个分隔符,所有SMTP电子邮件地址都需要它。

用户可以构建一个程序来提取电子邮件的用户名和域名,在Python中创建一个电子邮件切片机。更好的是,你可以添加你自己的定制,并将这些信息包含在你发送给主机的邮件中。虽然这是一个简单明了的项目想法,但对于提高你的编码能力是必不可少的。你甚至可以用Tkinter为它添加一个前端。

然而,这个项目的一个基本组成部分将是如下。

email = input("Enter Your Email: ").strip()
username = email[:email.index('@')]
domain = email[email.index('@') + 1:]

如何用Python制作一个YouTube视频下载器?

从事YouTube视频下载器项目是开始探索你的儿童Python动手项目的最佳方式之一。这是如何以愉快的方式向新手教授Python的最好说明。每个月,有超过10亿人观看YouTube。我们偶尔喜欢永久地下载某些视频。YouTube不提供这个选项,但你可以开发一个应用程序,让你可以下载各种文件类型和视频质量的YouTube视频。这个项目看起来很难,但一旦你开始,它就很简单。

在开始这个项目之前,你必须为Python安装pytube模块。Python没有附带这个模块,但可以通过运行下面的命令轻松安装。

pip install pytube

pytube模块允许你根据可以提供的视频链接来下载youtube视频。此外,它允许你选择你想下载的视频的分辨率。

要创建一个简单的Youtube下载器项目,首先要让用户输入本程序的视频链接。接下来,通过传递链接作为参数来创建YouTube模块的对象,之后你可以获得适当的视频扩展名和分辨率。你可以根据你的需要改变文件的名称;否则,将保留原来的名称。最后,使用下载功能,该功能有一个指定文件位置的参数,接下来就可以下载该文件。

# importing the module
from pytube import YouTube

# Setting the save path
SAVE_PATH = "C:/" #to_do

# Inputting link of the youtube video to be downloaded
link = input("Video Link:")

try:
	# Creating object using YouTube
	yotube = YouTube(link)
except:
	print("Unable to Connect!") # Exception handling

# Filter out all files with the "mp4" extension
mp4files = youtube.filter('mp4')

#to set the name of the file
youtube.set_filename('Sample Video')

# Video resolution to download passed in the get() function
d_video = youtube.get(mp4files[-1].extension, mp4files[-1].resolution)
try:
	# Downloading the youtube video
	d_video.download(SAVE_PATH)
except:
	print("Error while downloading!")
print('Download Completed!')

如何用Python计算斐波那契数列?

当你输入一个数字时,你可以使用一个函数来确定一个给定的数字是否是斐波那契数列的一部分。

前面的项目都有一个共同的特点,就是协助你正确地掌握基本原理。开发者和错误修复者都将是你。更不用说,你还将与变量、字符串、数字、运算符等打交道,并编写和实现各种函数。

对于这个程序,我们将在斐波那契数列中进行循环。下面是这个项目的一个算法。

  1. 要求用户输入一个数字来检查斐波那契数列。
  2. 使用前两个斐波那契项,在斐波那契序列中循环,直到达到一个高于或等于输入数的数字。
    1. 如果数字相等,用户的输入-输出就是斐波那契数列的一部分。
    2. 如果我们达到的数字比输入的大,则输出用户输入的数字不是斐波那契数列的一部分。

下面是一个示例代码的样子。

fib_terms = [0, 1]  # Defining the first two numbers of the sequence

user_input= int(input('Enter the number you want to checkn'))

# Adding new fibonacci terms until the user_input is reached
while fib_terms[-1] <= user_input:
    fib_terms.append(fib_terms[-1] + fib_terms[-2])

if user_input in fib_terms:
    print('Yes. ' + user_input + 'is NOT a fibonacci number.')
else:
    print('No. ' + user_input + 'is NOT a fibonacci number.')

我应该使用哪个 IDE 来创建和构建这些初级 Python 示例程序?

如果你是初学者,PyScripter 将是你的最佳选择。PyScripter 起初是一个直接的 IDE,为 Delphi 应用程序提供了一个可靠的脚本选项,以补充伟大的 Python for Delphi (P4D) 组件。它提供了一个更现代的用户界面,并且比其他IDE更快,因为它是用编译语言设计的。作为一个优秀的Python开发环境,它还提供了许多附加功能。

这个令人难以置信的IDE旨在开发一个可以与其他语言的基于Windows的传统IDE相媲美的Python IDE。PyScripter是一个非常有用的工具,因为它结构紧凑、适应性强、功能丰富。PyScripter从头到尾都是为Windows设计的,与繁琐的文本编辑器、万能的IDE或其他Python跨平台IDE相比,它明显更快,反应更快。

括号高亮、代码折叠、代码补全和输入时的语法检查是PyScripter的一些有用功能。一个程序员如果想掌握这个软件,就必须看看这个。通过使用Python源代码工具,程序员的使用简单性得到了加强。最后,使用这个IDE接受从资源管理器中投放的文件,可以节省时间。

最好的Python集成开发环境使项目经理能够导入预先存在的目录和众多运行配置。由于其集成的单元测试,工作更有成效,因为它可以创建自动测试。这个程序中的单元测试GUI很复杂。为了提高产品的质量,程序员可以将PyScripter与PyLint、TabNanny、Profile等Python工具结合使用。值得注意的是,IDE提供了一个强大的参数系统来调整外部工具的集成。

点击这里,开始用顶级的Python开发环境开发令人愉快和简单的项目。

The postTop 10 Fun And Easy Python Projects for Beginners In 2022first appeared onPython GUI.