用python写的好玩的小程序,python简单易懂的小程序

大家好,小编来为大家解答以下问题,用python写的好玩的小程序,python简单易懂的小程序,现在让我们一起来看看吧!

人生苦短,快学Python!

在使用Python的过程中,我最喜欢的就是Python的各种第三方库,能够完成很多操作。

下面就给大家介绍5个通过Python构建的项目,以此来学习Python编程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(fCPU:{cpu_score})
        print(fPlaer:{player_score})
        break
    else:
        print(That's not a valid play. Check your spelling!)
    computer = random.choice(choices)

二、随机密码生成器

目标:创建一个程序,可指定密码长度,生成一串随机密码。

提示:创建一个数字+大写字母+小写字母+特殊字符的字符串。根据设定的密码长度随机生成一串密码。

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

三、骰子模拟器

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

提示:当用户询问时,使用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

四、自动发送邮件

目的:编写一个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

五、闹钟

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

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

四、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

五、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。


这份完整版的Python全套学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

Python资料、技术、课程、解答、咨询也可以直接点击下面名片,添加官方客服斯琪

  • 10
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好,我可以为你提供一个基于 Python Tkinter 模块的简单游戏示例,名为“打地鼠游戏”: ``` import tkinter as tk import random # 创建主窗口 root = tk.Tk() root.title("打地鼠游戏") # 设置窗口大小 WIDTH = 400 HEIGHT = 400 root.geometry(f"{WIDTH}x{HEIGHT}") # 创建游戏区域 canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT) canvas.pack() # 定义地鼠类 class Mole: def __init__(self): self.x = random.randint(50, WIDTH-50) self.y = random.randint(50, HEIGHT-50) self.radius = 30 self.color = "brown" self.speed = random.randint(1000, 2000) self.draw() def draw(self): self.shape = canvas.create_oval(self.x-self.radius, self.y-self.radius, self.x+self.radius, self.y+self.radius, fill=self.color) def hide(self): canvas.delete(self.shape) # 定义游戏函数 def start_game(): global score, moles score = 0 moles = [] for i in range(10): moles.append(Mole()) update_score() root.after(30000, end_game) def end_game(): global score, moles for mole in moles: mole.hide() canvas.create_text(WIDTH/2, HEIGHT/2, text=f"游戏结束!得分:{score}", font=("Arial", 24)) def update_score(): canvas.delete("score") canvas.create_text(50, 20, text=f"得分:{score}", font=("Arial", 16), anchor="nw", tag="score") def hit(event): global score, moles for mole in moles: if abs(event.x-mole.x) < mole.radius and abs(event.y-mole.y) < mole.radius: score += 10 update_score() mole.hide() moles.remove(mole) moles.append(Mole()) # 绑定事件 canvas.bind("<Button-1>", hit) # 创建开始按钮 button = tk.Button(root, text="开始游戏", command=start_game, font=("Arial", 20)) canvas.create_window(WIDTH/2, HEIGHT/2, window=button) # 运行主程序 root.mainloop() ``` 这个游戏创建一个主窗口,包含一个游戏区域和一个“开始游戏”按钮。游戏区域随机生成 10 个地鼠,玩家需要在 30 秒内点击地鼠来得分。当时间到或者玩家点击了所有地鼠后,游戏结束,显示得分。你可以根据自己的需求进行修改和扩展,例如增加难度、音效等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值