(python必背100源代码)(python 源代码)

(python必背100源代码)(python 源代码)

一、石头剪刀布游戏

目标:创建一个命令行游戏,游戏者可以在石头、剪刀和布之间进行选择,与计算机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脚本,可以使用这个脚本发送电子邮件。

提示: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")

更多项目源码,请继续关注小编。如果大家在学习中遇到困难,想找一个python学习交流环境,可以加入我们的python裙,关注小编,并私信“01”即可进裙,领取python学习资料,会节约很多时间,减少很多遇到的难题。

四、闹钟

目的:编写一个创建闹钟的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

五、天气应用

目的:编写一个Python脚本,接收城市名称并使用爬虫获取该城市的天气信息。

提示:你可以使用Beautifulsoup和requests库直接从谷歌主页爬取数据。

安装:requests,BeautifulSoup

from bs4 import BeautifulSoup

import requests

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36

(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

def weather(city):

city=city.replace(" ","+")

res = requests.get(f'https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8',headers=headers)

print("Searching in google......\n")

soup = BeautifulSoup(res.text,'html.parser')

location = soup.select('#wob_loc')[0].getText().strip()

time = soup.select('#wob_dts')[0].getText().strip()

info = soup.select('#wob_dc')[0].getText().strip()

weather = soup.select('#wob_tm')[0].getText().strip()

print(location)

print(time)

print(info)

print(weather+"°C")

print("enter the city name")

city=input()

city=city+" weather"

weather(city)

(python必背100源代码)(python 源代码)

最后多说一句,小编是一名python开发工程师,这里有我自己整理了一套最新的python系统学习教程,包括从基础的python脚本到web开发、爬虫、数据分析、数据可视化、机器学习等。想要这些资料的可以关注小编,并在后台私信小编:“01”即可领取

声明:我要去上班所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流,版权归原作者头秃python程序员所有,原文出处。若您的权利被侵害,请联系删除。

本文标题:(python必背100源代码)(python 源代码)
本文链接:https://www.51qsb.cn/article/dvjztb.html

(0)
打赏微信扫一扫微信扫一扫QQ扫一扫QQ扫一扫
上一篇2023-09-14

你可能还想知道

发表回复

登录后才能评论