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")
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个问题,我思考许久得不出答案,因此向你请教,还希望你能在百忙之中抽出一点时间,帮我解答疑惑。非常感谢作者: crossin先生 时间: 2018-3-7 14:26
你是if,不是elif,第一个if执行完,还会继续向下执行。你用断点或者print仔细看看程序是怎样一步一步执行的,理解下执行的顺序