设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 6327|回复: 1
打印 上一主题 下一主题

pytnon3,请教编码的错误问题

[复制链接]

2

主题

0

好友

12

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2018-3-7 10:01:01 |只看该作者 |倒序浏览
C大,这是我学习之后自己尝试改良的一个猜数游戏,代码能够执行,但是我却遇到了些逻辑问题,我自己无法判断原因,因此发在帖子中希望得到指点

预定设计功能:猜出设定数字,最多可以有5次机会。每猜完一次,显示剩余可用次数。

问题代码  :
《《answer = int(input())#第一次输入数值


num = 66
global  times,rest
times = 0
while times < 4:
    rest=4-times
    if answer<num:
        times+=1
        print("bad,too small\n"
              "you have only %d chances rest\n"
              "give me the new answer"%rest)
        print(times,rest)
        
        answer=int(input())
    if  answer>num:
        times+=1
        print("Unlucky,too much\n"
              "you have only %d chances rest\n"
              "Guess again"%rest)
        print(times,rest)
        answer=int(input())
    if  answer==num:
        print("not bad kids,you made yourself a surviver")
        break
if times == 4:
    print("Stupid pigs,you have no value to live,die Die DIE !!")   #如果全部次数用完还没有猜对,输出
else:
    print("NOW,you can go ,I'll be here for the next ,HAHAHAHAHA") 》》#否则输出本行

问题描述:
通过将本代码第一行的“输入”,改入while循环内部,并删去数字不正确时的判定语句中的“输入”,并修改相关参数。可以完美运行预设目标,如下
num=66
global  times,rest
times = 0
while times < 5:
    rest=4-times
    answer=int(input())
    if answer<num:
        times+=1
        print("bad,too small\n"
              "you have only %d chances rest\n"
              "give me the new answer"%rest)
        print(times,rest)
      
    if  answer>num:
        times+=1
        print("Unlucky,too much\n"
              "you have only %d chances rest\n"
              "Guess again"%rest)
        print(times,rest)
        
    if  answer==num:
        print("not bad kids,you made yourself a surviver")
        break
if times == 5:
    print("Stupid pigs,you have no value to live,die Die DIE !!")
else:
    print("NOW,you can go ,I'll be here for the next ,HAHAHAHAHA")

但是原错误代码中的问题产生原因,我却不知道,主要有
1,如果猜测中,每次的数字都大于或者小于预设数66,则代码正常工作,但如果其中某次错误的猜大或者小了,下一次猜小了或者猜大了,rest的数值就会出错,具体如下

give me your answer!
77
Unlucky,too much
you have only 4 chances rest
Guess again
1 4
55
bad,too small
you have only 3 chances rest
give me the new answer
2 3
77
Unlucky,too much
you have only 3 chances rest
Guess again
3 3
55
bad,too small
you have only 1 chances rest
give me the new answer
4 1
77
Unlucky,too much
you have only 1 chances rest
Guess again
5 1
55
NOW,you can go ,I'll be here for the next ,HAHAHAHAHA
可以看到当答案摇摆时,times计数正常,但是rest却会依旧显示之前的数字,每两次之后又会输出正确值,但依然重复之前的错误

2,再times计数正常的情况下,while 判定失效,以至于可以输入6次答案(虽然第6次时,已经退出循环,不再进行条件判定)(然而,本当第5次就已经推出循环了)
3,如果5次猜测全部比设定值大或者小,第5次输入后应当不进行判断,直接输出“Stupid…”语句。但是,如果第五次输入正常答案,则会多输出“not bad kids …”一句,如下
give me your answer!
88
Unlucky,too much
you have only 4 chances rest
Guess again
1 4
88
Unlucky,too much
you have only 3 chances rest
Guess again
2 3
85
Unlucky,too much
you have only 2 chances rest
Guess again
3 2
84
Unlucky,too much
you have only 1 chances rest
Guess again
4 1
66
not bad kids,you made yourself a surviver
Stupid pigs,you have no value to live,die Die DIE !!
同样的,这种情况也会发生在问题1,描述的摇摆猜测情形之中,如下
give me your answer!
77
Unlucky,too much
you have only 4 chances rest
Guess again
1 4
45
bad,too small
you have only 3 chances rest
give me the new answer
2 3
75
Unlucky,too much
you have only 3 chances rest
Guess again
3 3
36
bad,too small
you have only 1 chances rest
give me the new answer
4 1
88
Unlucky,too much
you have only 1 chances rest
Guess again
5 1
66
not bad kids,you made yourself a surviver
NOW,you can go ,I'll be here for the next ,HAHAHAHAHA
也就是说,实际上第五次之后,如果答案正确,while 会依旧执行一次整体判断。这理论上应该不会发生才对。
对于这3个问题,我思考许久得不出答案,因此向你请教,还希望你能在百忙之中抽出一点时间,帮我解答疑惑。非常感谢
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

沙发
发表于 2018-3-7 14:26:57 |只看该作者
你是if,不是elif,第一个if执行完,还会继续向下执行。你用断点或者print仔细看看程序是怎样一步一步执行的,理解下执行的顺序
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-11-22 06:00 , Processed in 0.022111 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部