5个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(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) 

二、随机密码生成器
目标:创建一个程序,可指定密码长度,生成一串随机密码。
提示:创建一个数字+大写字母+小写字母+特殊字符的字符串。根据设定的密码长度随机生成一串密码。

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  
  • 10
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
猜数字游戏是一个非常经典的小游戏,不仅能够帮助我们锻炼编程能力,还能够提高我们的逻辑思维能力。下面我们就来探究一下 Python 猜数字游戏的实现方法和相关技巧。 首先,我们需要生成一个随机数,可以使用 Python 的 `random` 模块来实现。例如,要生成一个 1~100 之间的随机数,可以使用以下代码: ```python import random number = random.randint(1, 100) ``` 接下来,我们需要编写一个循环来让用户反复猜测,直到猜对为止。可以使用 Python 的 `while` 循环来实现。例如: ```python while True: guess = int(input('请猜一个1~100之间的数字:')) if guess == number: print('恭喜你猜对了!') break elif guess > number: print('你猜的数字太大了,请重新猜测!') else: print('你猜的数字太小了,请重新猜测!') ``` 在循环中,我们首先获取用户输入的数字,然后判断用户的输入是否与随机数相等。如果相等,就输出恭喜猜对了的信息,并且使用 `break` 语句跳出循环;如果不相等,就根据用户输入的数字与随机数的大小关系,输出猜测结果,让用户继续猜测。 需要注意的是,用户输入的数字是字符串类型,需要使用 `int()` 函数将其转换为整数类型。 除了基本的猜数字游戏之外,我们还可以进行一些功能的扩展,例如: - 增加猜测次数限制,超过次数还没猜对就结束游戏; - 增加难度,让随机数的范围更大; - 记录用户的成绩,计算猜对所需的次数和用时等信息。 总之,Python 猜数字游戏是一个非常有趣和实用的小项目,可以帮助我们提高编程能力和逻辑思维能力,也可以作为一个小休闲来放松一下自己。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

创作者mateo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值