我对Python还很陌生。我遇到的一个问题是使用sys.stdin.readline()函数。我的代码如下:con = sys.stdin.readline()。在此之后,我写了一个if语句来查看新数据。无论我输入什么内容,输出结果都相同。以下是我使用的代码:
import sys
print('Nice and quick what\'s your name?')
name = sys.stdin.readline()
print('What a great name!')
def moon_weight(weight, gain):
for year in range(1,16):
weight = weight + gain
moon = weight * 0.165
if year == 15:
print()
print('Year %s: Your weight on the moon is %s' % (year, moon))
print()
print('That\'s the end of this program for you, %s' % name)
print('Have a nice day bye!')
else:
print()
print('Year %s: Your weight on the moon is %s' % (year, moon))
print()
print('Press enter to see the next year...')
print('If you would not like to see the next year type no')
nex = sys.stdin.readline()
if nex == 'no':
print()
print('Ok, ending program now.')
print('Ending...')
break
moon_weight(55,1)
当我运行代码并输入no时,代码会继续执行,就好像我什么都没有输入一样。
input('What is your name')
来获取用户的名称,而不是使用sys.stdin.readline()
。例如,对于nex
变量也是如此。 - PrimeBeat