2021-04-21

python 迷你程序 (1)

1、骰子模拟器

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:
# 5
# 4

2、石头剪刀布

import random
choices = ["石头", "布", "剪刀"]
computer = random.choice(choices)
player = False
cpu_score = 0
player_score = 0
while True:
    player = input("石头,布 or 剪刀?:").capitalize()
    # 判断游戏者和电脑的选择
    if player == computer:
        print('平局!')
    elif player == "石头":
        if computer == "布":
            print("player输了")
            cpu_score += 1
        else:
            print("player赢了")
            player_score += 1
    elif player == "剪刀":
        if computer == "石头":
            print("player输了")
            cpu_score += 1
        else:
            print("player赢了")
            player_score += 1
    elif player == "布":
        if computer == "剪刀":
            print("player输了")
            cpu_score += 1
        else:
            print("player赢了")
            player_score += 1
    elif player == 'E':
        print('Final Scores:')
        print(f"CPU:{cpu_score}")
        print(f"Player:{player_score}")
        break
    else:
        print("This is not a valid word.Check your spelling")
    computer = random.choice(choices)
# 石头,布 or 剪刀?:石头
# player赢了
# 石头,布 or 剪刀?:E
# Final Scores:
# CPU:0
# Player:1

3、邮件地址切片器

email = input("请输入你的邮箱:").strip()
user_name = email[:email.index("@")]
domain_name = email[email.index("@")+1:]
result = f"你的用户名是{user_name},你的域名是{domain_name}"
print(result)
# 请输入你的邮箱:1147567638@qq.com
# 你的用户名是1147567638,你的域名是qq.com

4、自动发送邮件

import smtplib
from email.message import EmailMessage
email = EmailMessage()
email['from'] = 'rose'
email['to'] = '123456'
email['subject'] = 'welcome'
email.set_content('Welcome to China!!!')
with smtplib.SMTP(host='smtp.gmail.com', port=587) as smtp:
    smtp.ehlo()
smtp.starttls()
smtp.login("email_id", "Password")
smtp.sendmail(email)
print("The mail has been sent")

5、读女友来信 —— 冒险游戏

name = str(input("输入你的名字:"))
print(f'{name} 你的女友给你发了一条短信,询问你是否下课后和她一起吃饭。你的任务是回答让你的女友开心')
print("现在回答开始")
print("1.当然去了 2.当然去了,honey!")
response = int(input("选择一个数字 1 or 2:"))
if response == 1:
    print("女友现在很生气")
elif response == 2:
    print("女友开心了")
else:
    print("你死定了!")
# 输入你的名字:ll
# ll 你的女友给你发了一条短信,询问你是否下课后和她一起吃饭。你的任务是回答让你的女友开心
# 现在回答开始
# 1.当然去了 2.当然去了,honey!
# 选择一个数字 1 or 2:4
# 你死定了!

6、Hangman游戏 —— 一个猜单词的双人游戏

import time
import random
name = input("输入你的名字:")
print("你好" + name, "欢迎来到Hangman游戏!!")
time.sleep(1)
print("开始游戏!\n")
time.sleep(0.5)
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("\n你赢了!!")
        break
    guess = input("\n guess a character:")
    guess += guess
    if guess not in word:
        turns -= 1
        print("\n错误")
        print("\n你还有", + turns, "次机会")
        if turns == 0:
            print("\n你输了...")
# 输入你的名字:l
# 你好l 欢迎来到Hangman游戏!!
# 开始游戏!
# ________
# guess a character:pp
# 错误
# 你还有 4 次机会

本文内容大多来自于 公众号 —— python数据之道

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值