赞
踩
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 次机会
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。