[英]sys.stdin.readline() with if else (Python)
我是Python的新手,需要一點幫助。 我試圖使用下一個命令以便從屏幕上獲取用戶輸入:sys.stdin.readline()
當我想打印某些東西時一切都很好,但是當我嘗試合並一個if else語句時,似乎用戶輸入忽略了我在if ==中編寫的區分大小寫的字符串,即使在編寫時也總是返回else輸入“ Sam”
我想做一個簡單的任務,像這樣:
print("What is your name ?")
name = sys.stdin.readline()
print("Hello", name)
if name == 'Sam' :
print ('You are in our group')
else :
print('You are not in our group')
我應該在sys.stdin.readline()中做什么來確認if ==參數?
謝謝你的幫助
該行將包含行尾字符'\\n'
( docs )。 因此,它永遠不會等於'Sam'
(可能在文件末尾除外)。
也許使用name = name.strip()
刪除它和任何多余的空白字符。
關於主要職位:
我在同一代碼中使用了sys.stdin.readline()和input()。 兩次都工作。 以下是代碼:
import sys
print('how much was your meal')
price = int(input())
print('how much would you like to tip in percentage')
tip = int(input())
tipamount = price * (tip/100)
print('your tip amount is %s' %tipamount + 'dollars')
第二個代碼:
#import sys
#price = int(sys.stdin.readline())
#print('how much would you like to tip in percentage')
#tip = int(sys.stdin.readline())
#tipamount = price * (tip/100)
#print('your tip amount is %s' %tipamount + 'dollars')
但是,當我在以下代碼中使用sys.stdin.readline時,它不起作用。 相反,input()起作用了。 有人可以解釋為什么sys.stdin.readline在第一個代碼中起作用而在這個代碼中不起作用嗎?
import sys
print('are you looking to study digital media in australia?')
answer = str(sys.stdin.read())
if answer == 'yes':
print('Deakin University has one of the best Digital Science Program!')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.