鱼C论坛

 找回密码
 立即注册
查看: 1204|回复: 2

[已解决]猜数字小游戏运行出错,求指正

[复制链接]
发表于 2018-4-4 20:13:22 | 显示全部楼层 |阅读模式

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

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

x
我写的代码如下:
  1. print('---Guess a Number (in-between 1 to 100)---')
  2. print('5 attempts allowed.')
  3. guess = input('Please type a number: ')
  4. temp = int(guess)
  5. n=4
  6. if temp==66:
  7.       print('Correct!\nBut no reward.')
  8. while temp!=66 and n>0:
  9.       print(n, 'attempts left.')
  10.       n=n-1
  11.       if temp>66 and n>=0:
  12.             guess = input('Go down a little bit: ')
  13.             temp = int(guess)
  14.       if temp<66 and n>=0:
  15.             guess = input('Go up a little bit: ')
  16.             temp = int(guess)
  17.       if n==0 and temp!=66:
  18.             print('No attempt left.')
  19.       if temp==66:
  20.             print('Correct!\nBut no reward.')
  21. print('Game Over')
复制代码


运行时会出现以下红色部分这种情况,求指正
---Guess a Number (in-between 1 to 100)---
5 attempts allowed.
Please type a number: 50
4 attempts left.
Go up a little bit: 75
3 attempts left.
Go down a little bit: 65
Go up a little bit: 70

2 attempts left.
Go down a little bit:
最佳答案
2018-4-4 20:21:46
if temp>66 and n>=0:
            guess = input('Go down a little bit: ')
            temp = int(guess)
      elif temp<66 and n>=0:
            guess = input('Go up a little bit: ')
            temp = int(guess)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-4-4 20:21:46 | 显示全部楼层    本楼为最佳答案   
if temp>66 and n>=0:
            guess = input('Go down a little bit: ')
            temp = int(guess)
      elif temp<66 and n>=0:
            guess = input('Go up a little bit: ')
            temp = int(guess)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2018-4-4 23:25:38 | 显示全部楼层
ba21 发表于 2018-4-4 20:21
if temp>66 and n>=0:
            guess = input('Go down a little bit: ')
            temp = int(gu ...

非常感谢,受教了不过刚开始看到答案还以为elif要比if多缩进一个tab
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-1 04:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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