鱼C论坛

 找回密码
 立即注册
查看: 122|回复: 1

[作品展示] 现在看到第十四节课了,总结一下游戏吧并总结代码加平时练习

[复制链接]
发表于 前天 12:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
现在还是很懵逼的状态,不说了,分享:

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)
   
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 前天 12:57 | 显示全部楼层
看到你分享的代码了,我来帮你梳理一下游戏结构和代码要点:

游戏结构总结
1. 登录验证(3次尝试机会)
2. 数字猜谜(1-10随机数,3次机会)
3. 随机抽签(需完善options列表)
4. 闰年判断
5. 猜拳游戏
6. 取偶数游戏(代码不完整)

代码问题修正
1. options列表未定义 → 需添加如:
  1. options = ["大吉","中吉","小吉"]
复制代码

2. 闰年判断后的print("-"*40)缩进错误(多缩进一级)
3. 取偶数游戏部分代码不完整(需要补充逻辑)

核心代码片段
  1. python
  2. # 登录验证
  3. while count < 3:
  4.     if input_name == username and input_pass == password:
  5.         print("登陆成功")
  6.         break

  7. # 数字猜谜
  8. while counts < 3:
  9.     guess = int(input(f"请输入(1-10),还有{3-counts}次机会:"))
  10.     if guess == secret:
  11.         print("恭喜猜对")
  12.         break
  13.     elif guess > secret:
  14.         print("大了")
  15.     else:
  16.         print("小了")

  17. # 闰年判断(简化版)
  18. if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
  19.     print(f"{year}是闰年")
  20. else:
  21.     print(f"{year}不是闰年")

  22. # 猜拳游戏(需在开头添加)
  23. choices = ["石头", "剪刀", "布"]
  24. computer = random.choice(choices)
  25. # 胜负判断逻辑见原代码
复制代码


练习建议
  • 把每个游戏拆分成独立函数
  • 给所有变量起有意义的名字(如count→login_attempts)
  • 用函数封装重复操作(如数字校验)
  • 添加异常处理 try-except


你现在的困惑很正常!建议:
1. 先完成options列表定义
2. 修复闰年判断后的缩进
3. 单独测试每个小游戏功能
4. 用#注释标记各游戏模块

遇到具体问题随时问我,从报错信息开始解决最有效率!加油!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-5 22:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表