Python使用if语句读取来自sys.stdin.readline()的数据

3

我对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时,代码会继续执行,就好像我什么都没有输入一样。

2
我可以建议使用input('What is your name')来获取用户的名称,而不是使用sys.stdin.readline()。例如,对于nex变量也是如此。 - PrimeBeat
@PrimeBeat 谢谢!更改后它完美地工作了! - Kbeast
3个回答

4

sys.stdin.readline()返回字符串"no\n"

通过python交互式解释器测试sys.stdin.readline() == 'no'将返回False

您可以尝试使用if "no" in sys.stdin.readline():

问题在于您正在查找精确的字符串"no"。 因此,即使使用input(),如果用户添加了空格或键入"No""No!""NO"等,也可能会出现问题。

因此,您首先必须规范化您的字符串。

if "no" in sys.stdin.readline().strip().lower():

可能是一个简单的近似解。

顺便说一下,如果您的变量名与关键字冲突,您可以使用下划线,如next__next


2
请使用输入而不是使用输出。
import sys
print('Nice and quick what\'s your name?')
name = input()
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 = input()

        if nex == 'no':
            print()
            print('Ok, ending program now.')
            print('Ending...')
            break
                    
moon_weight(55,1)

2
除了以上的评论,还要考虑对代码进行重构,在实际计算方面只有一个地方:
def moon_weight(weight, gain, name, year_limit=16):
    for year in range(1, year_limit):
        weight += gain
        moon = weight * 0.165
        # This is the actual calculation. No need to repeat it twice
        print('Year %s: Your weight on the moon is %s.' % (year, moon))
        answer = input('Press enter to see the next year or type "no" to stop:')
        # This is the only branching logic for the quick stop
        if answer == 'no':
            print('Ok, ending program now.')
            print('Ending...')
            break
    else: # Special construct for the loop that runs if there was no break.
        print('That\'s the end of this program for you, %s' % name)
        print('Have a nice day bye!')


if __name__ == "__main__":
    name = input("What's your name?\n")
    moon_weight(55, 1, name, 5)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接