程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Fishers always have 5 Python Mini projects. Playing all day is not a problem (source code attached)

編輯:Python

Hello, everyone , I'm a panda

In the use of Python In the process of , My favorite is Python A variety of third-party libraries , Can do a lot of things .

Let's introduce 5 through Python Projects built , To learn Python Programming .

One 、 Stone scissors and paper games

The goal is : Create a command line game , Players can play in stone 、 Choose between scissors and cloth , With computers PK. If the player wins , The score will add , until

At the end of the game , The final score will be shown to the player .

Tips : Receive player's choice , And compared with the choice of the computer . The computer's selection is randomly selected from the selection list . If the player wins , Increase 1 branch .

 Welcome to white whoring Q Group :660193417###
import random
choices = ["Rock", "Paper", "Scissors"]
computer = random.choice(choices)
player = False
cpu_score = 0
player_score = 0
while True:
player = input("Rock, Paper or Scissors?").capitalize()
# Judge the choice of players and computers
if player == computer:
print("Tie!")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
cpu_score+=1
else:
print("You win!", player, "smashes", computer)
player_score+=1
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
cpu_score+=1
else:
print("You win!", player, "covers", computer)
player_score+=1
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
cpu_score+=1
else:
print("You win!", player, "cut", computer)
player_score+=1
elif player=='E':
print("Final Scores:")
print(f"CPU:{cpu_score}")
print(f"Plaer:{player_score}")
break
else:
print("That's not a valid play. Check your spelling!")
computer = random.choice(choices)

Two 、 Random code generator

The goal is : Create a program , Password length can be specified , Generate a string of random passwords .

Tips : Create a number + Capital + Lowercase letters + Special character string . Randomly generate a string of passwords according to the set password length .

 Welcome to white whoring Q Group :660193417###
import random
passlen = int(input("enter the length of password" ))
s=" abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKL MNOPQRSTUVIXYZ!aN$x*6*( )?"
p = ".join(random.sample(s,passlen ))
print(p)
----------------------------
enter the length of password
6
Za1gB0

3、 ... and 、 Dice simulator

Purpose : Create a program to simulate a dice roll .

Tips : When the user asks , Use random The module generates a 1 To 6 Number between .

 Welcome to white whoring Q Group :660193417###
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

Four 、 Send Email automatically

Purpose : Write a Python Script , You can use this script to send email .

Tips :email Libraries can be used to send e-mail .

 Welcome to white whoring Q Group :660193417###
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

Purpose : Write a program to create an alarm clock Python Script .

Tips : You can use date-time Module to create alarm clock , as well as playsound The library plays sound .

 Welcome to white whoring Q Group :660193417###
from datetime import datetime
from playsound import playsound
alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")
alarm_hour=alarm_time[0:2]
alarm_minute=alarm_time[3:5]
alarm_seconds=alarm_time[6:8]
alarm_period = alarm_time[9:11].upper()
print("Setting up alarm..")
while True:
now = datetime.now()
current_hour = now.strftime("%I")
current_minute = now.strftime("%M")
current_seconds = now.strftime("%S")
current_period = now.strftime("%p")
if(alarm_period==current_period):
if(alarm_hour==current_hour):
if(alarm_minute==current_minute):
if(alarm_seconds==current_seconds):
print("Wake Up!")
playsound('audio.mp3') ## download the alarm sound from link
break

Last

That's the end of today's sharing , That's what I want to share with you 5 A mini project , Learned to play all day will not be tired , If you don't believe it, try it . Last, last , If you like it, remember to give a five-star praise , If you don't understand, remember to comment , I'll reply when I see it .

I'm a panda , See you in the next article


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved