欢迎光临~湖南智能应用科技有限公司-hniat.com
语言选择: 中文版 ∷  英文版

基础知识

适合Python练手的5个经典项目,有趣又实用,想入门拿高薪的必看!

前言

各位百度朋友们好啊!我是斯琪!

希望能通过我的分享,给大家带来一些正能量,在放弃边缘的朋友看到了,能继续坚持,坚持努力的朋友看到了,能变得更优秀!

废话不多说,以下就是实打实的资料+分享,请耐心看完。

一、自动发送邮件

用Python编写一个可以发送电子邮件的脚本。

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

import email 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.gamil.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(猜单词的游戏)

用Python创建一个简单的hangman猜单词游戏。

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

import time import random
    name = input("What is your name?")  time.sleep(1) print ("Start guessing...\n")  time.sleep(0.5) ## A list Of Secret Words words = ["python","programming ","treasure ","creative"," medium","horror"]  words = random.choice(words)
    guesses = ''
    turns = 5 import 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:") guess += guess if guess not in word
                turns -= 1 print("\nWrong") print("\nYou have",) + turns,'more guesses' if turns == 0 print ("\nYou Lose")  

三、闹钟

用Python编写一个创建闹钟的脚本。

提示:用date-time模块创建闹钟,然后用playsound库播放声音。

    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 

四、石头剪刀布游戏

创建一个石头剪刀布的游戏,游戏者与与计算机PK。如果游戏者赢了,得分就会添加,看谁最终的得分最高。

提示:先判断游戏者的选择,然后与计算机的选择进行比较。计算机的选择是从选择列表中随机选取的。如果游戏者获胜,则增加1分。

    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() # 判断电脑与游戏者的选择 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)

五、提醒小工具

利用Python一个提醒小工具,在设定好的时间在桌面做提醒通知。

提示:跟踪提醒时间可以用Time模块,显示桌面通知可以用toastnotifier库。

安装:win10toast

    from win10toast import ToastNotifier import time
    toaster = ToastNotifier() try: print("Title of reminder") header = input() print("Message of reminder") text = input() print("In how many minutes?") time_min = input() time_min=float(time_min) except: header = input("Title of reminder\n") text = input("Message of remindar\n") time_min=float(input("In how many minutes?\n")) time_min = time_min * 60 print("Setting up reminder..") time.sleep(2) print("all set!") time.sleep(time_min)
    toaster.show_toast(f"{header}", f"{text}",
    duration=10, threaded=True) while toaster.notification_active(): time.sleep(0.005) 

结语

今天的分享的有些小难度了(对新手来说),如果你还是纯Python新手,建议先加上我的微信

img
微信扫一扫
免费领取python入门课程或资料
关闭
用手机扫描二维码关闭
二维码