现在看到第十四节课了,总结一下游戏吧并总结代码加平时练习
现在还是很懵逼的状态,不说了,分享:username = "123"
password = "123"
import random
secret = random.randint(1,10)
count = 0
counts = 0
oks = "ok"
ev = 0
en = 0
whe = 1
at = 0
while count < 3:
input_name = input("请用户输入账号:")
input_pass = input("请用户输入密码:")
if input_name == username and input_pass == password:
print("登陆成功")
plays = input("请确定进入:")
if plays == oks:
while counts < 3:
guess = input(f"请输入你猜的数字(1-10),你还有{ 3 - counts}机会:")
while not guess.isdigit():
guess = input("抱歉,您的输入有误,请输入一个整数:")
guess = int(guess)
if guess < 1 or guess > 10:
print("请输入1-10之间的数字!")
continue
counts += 1
if guess == secret:
print("恭喜你,猜对了")
break
elif guess > secret:
print("猜错了,大了")
else:
print("猜错了,小了")
if counts == 3:
print(f"游戏结束!正确答案是{secret}")
print("-" * 40)
print("现在可以进行下一个游戏了哦,开心吗?")
input("大声告诉我你现在的感受:")
options = ["大吉","大福","横财"]
result = random.choice(options)
print("你本次抽到的签是:",result)
print(f"你很幸运,抽中{result},恭喜你哦,如果你愿意,我们可以进行下一个知识点.")
print("-" * 40)
leap = input("你想知道哪一年是闰年吗?想知道输入ok哦.您的输入是:")
if leap == oks:
year = input("请输入一个年份:")
while not year.isdigit():
year = input("抱歉,你输入有误,请输入一个整数:")
year = int(year)
if year % 400 == 0:
print(year, "是闰年!")
else:
if year % 4 == 0 and year % 100 != 0:
print(year, "是闰年!")
else:
print(year, "不是闰年!")
print("-" * 40)
print("非常棒,现在我们继续下一个游戏")
print("我们一起玩一个猜拳游戏吧")
choices = ["石头","剪刀","布"]
computer = random.choice(choices)
player = input("请出拳(石头/剪刀/布):")
print("你出的是:",player)
print("电脑出的是:",computer)
if player == computer:
print("平局")
elif (player == "石头" and computer == "剪刀") or\
(player == "剪刀" and computer == "布") or\
(player == "布" and computer == "石头"):
print("你赢了,太棒了")
else:
print("您输了")
print("-" * 40)
print("现在我们复习一下取偶数游戏")
while ev <= 1000000:
if ev % 2 == 0:
en += ev
ev += 1
print("1000000以内所有偶数的和是:", en)
print("-" * 40)
print("现在复习麦子游戏,循环计数N+64次方")
while whe <= 64:
wheats = pow(2, whe-1)
at += wheats
whe += 1
print("大王应该给农民",at,"麦粒子")
break
else:
print("老规矩,不OK退出了哦")
break
else:
print("不ok自动退出了哦")
break
else:
print("用户名或者密码错误")
count += 1
if count == 3:
print("错误三次,账户锁定")
print("-" * 40)
看到你分享的代码了,我来帮你梳理一下游戏结构和代码要点:
游戏结构总结
1. 登录验证(3次尝试机会)
2. 数字猜谜(1-10随机数,3次机会)
3. 随机抽签(需完善options列表)
4. 闰年判断
5. 猜拳游戏
6. 取偶数游戏(代码不完整)
代码问题修正
1. options列表未定义 → 需添加如:options = ["大吉","中吉","小吉"]
2. 闰年判断后的print("-"*40)缩进错误(多缩进一级)
3. 取偶数游戏部分代码不完整(需要补充逻辑)
核心代码片段
python
# 登录验证
while count < 3:
if input_name == username and input_pass == password:
print("登陆成功")
break
# 数字猜谜
while counts < 3:
guess = int(input(f"请输入(1-10),还有{3-counts}次机会:"))
if guess == secret:
print("恭喜猜对")
break
elif guess > secret:
print("大了")
else:
print("小了")
# 闰年判断(简化版)
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(f"{year}是闰年")
else:
print(f"{year}不是闰年")
# 猜拳游戏(需在开头添加)
choices = ["石头", "剪刀", "布"]
computer = random.choice(choices)
# 胜负判断逻辑见原代码
练习建议
[*]把每个游戏拆分成独立函数
[*]给所有变量起有意义的名字(如count→login_attempts)
[*]用函数封装重复操作(如数字校验)
[*]添加异常处理 try-except
你现在的困惑很正常!建议:
1. 先完成options列表定义
2. 修复闰年判断后的缩进
3. 单独测试每个小游戏功能
4. 用#注释标记各游戏模块
遇到具体问题随时问我,从报错信息开始解决最有效率!加油!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 勉励自己,加油 下次可以用专用的代码格式发代码{:10_275:} cjjJasonchen 发表于 2025-7-4 17:26
下次可以用专用的代码格式发代码
哈哈哈,好.不知道有这个功能
页:
[1]