制作一个跟电脑对战的石头剪刀布小游戏(电脑随机出招),进入游戏时候提示当前时间,并需要登录密码。
密码规则:
总共是11位,全部由数字组成,第1位是1,第二位可以是数字3458其中之一,后面9位任意数字
例如:18601012345、13912367890则满足。
要求:
- 执行程序,进行友好的提示,需要提示当前时间
- 判断密码正确性
- 密码错误则重新输入
- 密码正确进入游戏循环,游戏具有友好的提示
- 出招处理胜负!0退出游戏
import random
import sys
import time
cq = ['石头','剪刀','布'] # 定义一个猜拳列表
while True: # 无限循环
local_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) # 获取当前时间
print(f'欢迎来到猜拳游戏!当前时间是:{local_time}') # 输出当前时间
pwd = input("请输入密码登录:") # 定义一个输入密码语句
if len(pwd) == 11 and pwd.isdigit() and pwd[0] =='1' and pwd[1] in '3458': # 判断所输入的密码是否符合要求 isdigit():检查pwd是否全为数字
print('欢迎登录,可以进行猜拳了')
print('====================')
while True:
user = input('请输入猜拳(0退出游戏):') # 定义一个输入猜拳动作语句
computer = random.choice(cq) # 随机在猜拳列表中选出一个赋值给电脑
if user in cq: # 判断用户输入的猜拳动作是否在猜拳列表中,即猜拳动作是否合理
if (user == '石头') and (computer =='剪刀') or (user =='剪刀') and (computer=='布') or (user=='布') and (computer=='石头'): # 判断用户赢了的情况
print(f'你赢了,电脑出的是{computer}')
elif user == computer: # 判断平局的情况
print(f'平局,电脑出的是{computer}')
else:
print(f'你输了,电脑出的是{computer}')
elif user=='0': # 判断是否输入退出程序的命令
print('你已退出程序') # 输入退出程序的命令后,提示
sys.exit() # 退出程序
else:
print('你出错招了') # 猜拳动作输入不合理时,提示
else:
print('你的密码有误,请重新输入!') # 密码输入有误时,提示
效果: