【简单又有趣】Python五个迷你小项目,即学即用,还不赶紧码住(附源码)

目录


前言

Python编程语言中,我最喜欢的就是Python的各种第三方库,能够完成很多操作。

下面就给大家介绍5个通过Python构建的项目,以此来学习Python编程。大家也可根据项目的目的及提示,自己构建解决方法,提高编程水平。


一、猜数字游戏

目的:在这个游戏中,任务是创建一个脚本,能够在一个范围内生成一个随机数。如果用户在三次机会中猜对了数字,那么用户赢得游戏,否则用户输。

提示:生成一个随机数,然后使用循环给用户三次猜测机会,根据用户的猜测打印最终的结果。

import random
number = random.randint(1,10)for i in range(0,3):
user = int( input( "guess the number" ))if user number:
print( "Hurray !!")
print( f"you guessed the number right it's {number}")break
elif user>number:
print( "Your guess is too high" )elif user<number:
print( "Your guess is too low." )
print( f"Nice Try !, but the number is {number}")

二、骰子模拟器

目的:创建一个程序来模拟掷骰子。

提示:当用户询问时,使用random模块生成一个1到6之间的数字。

import random;
while int(input( ' Press 1 to roll the dice or 0 to exit:\n')): print(random.randint(1,6))
.------------------------------------------
Press 1 to roll the dice or 0 to exit
1
4

三、故事生成器

目的:每次用户运行程序时,都会生成一个随机的故事。

提示:random模块可以用来选择故事的随机部分,内容来自每个列表里。

import random
when = [ 'A few years ago','Yesterday', 'Last night', 'A long time ago' , ' on 20th Jan']
who = [ 'a rabbit ', 'an elephant ', 'a mouse ', 'a turtle ' , 'a cat ']
name = [ 'Ali', 'Miriam' , 'daniel', 'Hoouk ', 'Starwalker']
residence = [ 'Barcelona ' , 'India', 'Germany', 'Venice', 'England ']
went = [ 'cinema ', 'university' , 'seminar', 'school', 'laundry ']
happened = ['made a lot of friends' , ' Eats a burger', 'found a secret key', 'solved a mistery','wrote a book ' ]
print( random.choice(when)+ ', ' + random.choice(who) + ' that lived in '+
random.choice(residence) + ', went to the ' + random.choice(went) + ' and ' +
random.choice(happened))
--------------------------------OUTPUT--------------------------.
A long time ago, a cat that lived in England,went to the seminar and solved a mistery

四、自动发送邮件

目的:编写一个Python脚本,可以使用这个脚本发送电子邮件。

提示:email库可用于发送电子邮件。

import smtplib 
from email.message import EmailMessage
email = EmailMessage() ## Creating a object for EmailMessage
email['from'] = 'xyz name'   ## Person who is sending
email['to'] = 'xyz id'       ## Whom we are sending
email['subject'] = 'xyz subject'  ## Subject of email
email.set_content("Xyz content of email") ## content of email
with smtlib.SMTP(host='smtp.gmail.com',port=587)as smtp:     
## sending request to server 
    smtp.ehlo()          ## server object
smtp.starttls()      ## used to send data between server and client
smtp.login("email_id","Password") ## login id and password of gmail
smtp.send_message(email)   ## Sending email
print("email send")    ## Printing success message

五、Hangman

目的:创建一个简单的命令行hangman游戏。

提示:创建一个密码词的列表并随机选择一个单词。现在将每个单词用下划线“_”表示,给用户提供猜单词的机会,如果用户猜对了单词,则将“_”用单词替换

import time
import random
name = input("What is your name? ")
print ("Hello, " + name, "Time to play hangman!")
time.sleep(1)
print ("Start guessing...\n")
time.sleep(0.5)
## A List Of Secret Words
words = ['python','programming','treasure','creative','medium','horror']
word = random.choice(words)
guesses = ''
turns = 5
while turns > 0:         
    failed = 0             
    for char in word:      
        if char in guesses:    
            print (char,end="")    
        else:
            print ("_",end=""),     
            failed += 1    
    if failed == 0:        
        print ("\nYou won") 
        break              
    guess = input("\nguess a character:") 
    guesses += guess                    
    if guess not in word:  
        turns -= 1        
        print("\nWrong")    
        print("\nYou have", + turns, 'more guesses') 
        if turns == 0:           
            print ("\nYou Lose") 


总结

以上就是今天分享的内容,针对上面这些项目,有些项目大家可以根据自己的需求进行适当调整。

猜你喜欢

转载自blog.csdn.net/weixin_69333592/article/details/128266718