簡體   English   中英

Python 使用 if 語句從 sys.stdin.readline() 獲取數據

[英]Python taking data from sys.stdin.readline() with if statements

我對 python 很陌生。 我遇到的問題之一是使用 sys.stdin.readline()。 我已經像這樣 con = sys.stdin.readline() 編寫了代碼。 完成此操作后,我放置了一個 if 語句來查看該新數據。 這樣做之后,無論我輸入什么 output 都是完全相同的。 這是我正在使用的代碼:

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 時,代碼會繼續運行,就像我什么都沒寫一樣。

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

改用輸入。

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)

除了上面的注釋之外,考慮重構你的代碼,讓它只有一個地方用於實際計算:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM