鱼C论坛

 找回密码
 立即注册
查看: 613|回复: 6

004作业问题

[复制链接]
发表于 2019-3-1 09:55:08 | 显示全部楼层 |阅读模式

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

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

x
作业:给予三次机会,猜1-10的数字,若猜三次仍然不对,则结束游戏。
代码如下:

import random
secret=random.randint(1,10)
times=3
guess=0
print('猜一个数:',end='')
while (guess!=secret) and (times > 0):
    guess=input()
    num=int(guess)
    times=times-1
    if num==secret:
        print('guess right')
        print('no reward')
    else:
        if num > secret:
            print('too large')
        else:
            print('too small')
        if times >0:
            print('try again:',end='')
        else:
            print('used up your chances')            
print(' the end of the game')


我在运行过程中,发现只有运行三次都错,才会出现最后一行: the end of the game
只要三次以内猜对,则不会运行最后一行 print(' the end of the game')
因为最后一行代码是和while并列的代码,所以我原本想的是,只要结束游戏,就会出现最后一行
不知道哪里出现了问题?感谢

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-1 10:01:22 | 显示全部楼层
import random
secret=random.randint(1,10)
times=3
guess=0
print('猜一个数:',end='')
while (guess!=secret) and (times > 0):
    guess=input()
    num=int(guess)
    times=times-1
    if num==secret:
        print('guess right')
        print('no reward')
        break
    else:
        if num > secret:
            print('too large')
        else:
            print('too small')
        if times >0:
            print('try again:',end='')
        else:
            print('used up your chances')            
print(' the end of the game')
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-1 10:03:48 | 显示全部楼层
(guess!=secret) and (times > 0)  你的条件的问题,还有你的guess开始是int后来输入就变成字符串了,一致就能用上条件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-1 10:30:48 | 显示全部楼层
正如二楼所加的,你在猜对的时候需要break出循环才能显示the end of the game。
如果你在第1次或者第2次猜对的话,你还需要输入2或1次才能结束循环,才能显示出the end of the game。
也就是说你在3次内猜对的时候,程序还在循环中,它还在等你输入值。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-1 10:48:25 | 显示全部楼层
塔利班 发表于 2019-3-1 10:03
(guess!=secret) and (times > 0)  你的条件的问题,还有你的guess开始是int后来输入就变成字符串了,一致 ...

感谢,我发现我在while语句中,把guess重新变成int后,程序就运行正常了。但是我有一点不懂,望指教:
guess已经变成字符串了,为什么程序没有报错而且还可以继续比较呢?
比如:
第一次:guess='4'  secret=5
在运行到
        if times >0:
            print('try again:',end='')
将返回到
while (guess!=secret) and (times > 0):
进行判断,
此时 guess是字符串,而secret是int,两者应该不能比较是否相等,报错才对呀。为什么还是可以继续运行代码呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-1 11:03:03 | 显示全部楼层
maogo 发表于 2019-3-1 10:30
正如二楼所加的,你在猜对的时候需要break出循环才能显示the end of the game。
如果你在第1次或者第2次猜 ...

谢谢,3楼指出了我另一个问题,就是guess一开始是整数型,后来input后变成了字符串,
于是我将:
while (guess!=secret) and (times > 0):
    guess=input()
    num=int(guess)
    times=times-1
改成:
while (guess!=secret) and (times > 0):
    temp=input()
    guess=int(temp)
    times=times-1
然后用guess和secret进行比较
发现这样做,即便不break,也可以运行到最后一行,所以想问一下为什么把guess改成整数型,就不存在这样的问题呢?感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-1 11:07:41 | 显示全部楼层
while条件只管漫步满足,不管对错,字符串不等于int所以满足条件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-11 05:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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