while中while该如何理解
刚学完零基础python第4讲,为了让反馈(大了还是小了)和input放在一行,自己改编了一个版本,虽然能正常运行,但不知道while中while是否合理,如果是该如何理解?而且这个如何改进或简化?temp = input("猜猜我现在在想哪个数字:")
guess = int(temp)
if guess == 666:
print('宁真棒!')
else:
while guess > 666:
temp = input('太大了,再试一下:')
guess = int(temp)
while guess < 666:
temp = input('太小了,换一个:')
guess = int(temp)
if guess == 666:
print('宁真棒')
while guess < 666:
temp = input('太小了,再试试:')
guess = int(temp)
while guess > 666:
temp = input('太大了,再换一个:')
guess = int(temp)
if guess == 666:
print('宁真棒')
print('不玩啦') 代码是从上向下读取的
可以运行证明逻辑没有问题,但是可读性非常差
temp = input("猜猜我现在在想哪个数字:")
guess = int(temp)
while guess != 666:
if guess < 666:
temp = input('太小了,再试试:')
else:
temp = input('太大了,再换一个:')
guess = int(temp)
else:
print('宁真棒')
print('不玩啦')
逃兵 发表于 2020-12-4 16:37
代码是从上向下读取的
可以运行证明逻辑没有问题,但是可读性非常差
哇,改的太好了,谢谢宁!
页:
[1]