5 Python Mini programs worth learning and practicing (with code)

In the process of using python, my favorite is Python's various third-party libraries, which can complete many operations.

Let's introduce five projects built through Python to learn python programming.

1, Stone scissors paper game

  goal: create a command line game where players can choose between stones, scissors and cloth, and PK with the computer. If the player wins, the score will be added until the end of the game, and the final score will be displayed to the player.

  tip: receive the player's selection and compare it with the computer's selection. The choice of the computer is randomly selected from the selection list. If the player wins, 1 point will be added.

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:  
    elif player == "Rock":  
        if computer == "Paper":  
            print("You lose!", computer, "covers", player)  
            print("You win!", player, "smashes", computer)  
    elif player == "Paper":  
        if computer == "Scissors":  
            print("You lose!", computer, "cut", player)  
            print("You win!", player, "covers", computer)  
    elif player == "Scissors":  
        if computer == "Rock":  
            print("You lose...", computer, "smashes", player)  
            print("You win!", player, "cut", computer)  
    elif player=='E':  
        print("Final Scores:")  
        print("That's not a valid play. Check your spelling!")  
    computer = random.choice(choices)

2, Random cipher generator

Objective: to create a program that can specify the password length and generate a string of random passwords.

  tip: create a string of numbers + uppercase letters + lowercase letters + special characters. Randomly generate a string of passwords according to the set password length.

import random  
passlen = int(input("enter the length of password" ))  
s=" abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKL MNOPQRSTUVIXYZ!aN$x*6*( )?"  
p = ".join(random.sample(s,passlen ))  
enter the length of password  

3, Dice simulator

Objective: to create a program to simulate dice rolling.

Tip: when the user asks, use the random module to generate a number between 1 and 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  

4, Send mail automatically

Purpose: write a Python script that you can use to send e-mail.

Tip: the email library can be used to send e-mail.

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 

5, Alarm clock

Purpose: write a Python script to create an alarm clock.

Tip: you can use the date time module to create an alarm clock and the playsound library to play sounds.

from datetime import datetime     
from playsound import playsound  
alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")  
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")  
                    print("Wake Up!")  
                    playsound('audio.mp3') ## download the alarm sound from link  

  experience in software testing, interface testing, automation testing, continuous integration and interview. If you are interested, you can go to 806549072. There will be irregular sharing of test data in the group. There will also be technology giants to exchange technology with peers in the industry

Tags: Python Pycharm pygame

Posted on Mon, 11 Oct 2021 18:19:36 -0400 by akreation