Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sys.stdin.readline()和if-else(Python)_Python - Fatal编程技术网

sys.stdin.readline()和if-else(Python)

sys.stdin.readline()和if-else(Python),python,Python,我是Python新手,需要一些帮助。 我试图使用下一个命令从屏幕获取用户输入: sys.stdin.readline() 当我想要打印一些东西时,一切都很好,但是当我尝试组合if-else语句时,用户输入似乎忽略了我在if==中编写的区分大小写的字符串,并且它总是返回else,即使在我编写输入“Sam”时也是如此 我想做一个像这样的简单任务: print("What is your name ?") name = sys.stdin.readline() print("Hello", nam

我是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'
()。所以它永远不等于
“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!')

您可以使用raw_input()函数而不是sys.stdin.readline(),因为它忽略了行尾字符('\n')@Jalo,看起来他们在使用python3,所以它将是
input('Prompt:')
。好的@Holloway,我没有注意到ethanks@RemcoGerlich!现在是山姆:)