如何使用标准输入和标准输出获取输入并显示它们的总和
How to take inputs and display their sum using stdin and stdout
我写了一个简单的代码来添加两个数字 stdin
和 stdout
分别获取输入和显示输出:
import sys
print("Inputs:")
for one in sys.stdin:
break
int(one)
for two in sys.stdin:
break
int(two)
three = 0
print("Output:")
three = one + two
sys.stdout.write(three)
我得到的输出是:
Inputs:
1
2
Output:
1
2
预期输出为 3
。但是我得到的是在上面的输出中显示的。
我尝试使用相同的代码 input()
:
one = int(input())
two = int(input())
three = one + two
print(three)
我得到的输出是 3
。我的第一个代码中缺少什么?
我想你想做的是:
aNumber = input('Enter a number: ')
anotherNumber = input('Enter another number: ')
print(int(aNumber) + int(anotherNumber))
要使用 stdin/out 执行此操作,您可以使用:
import sys
print("Inputs:")
one = sys.stdin.readline()
two = sys.stdin.readline()
print("Output:")
three = int(one) + int(two)
four = str(three)
sys.stdout.write(four)
我写了一个简单的代码来添加两个数字 stdin
和 stdout
分别获取输入和显示输出:
import sys
print("Inputs:")
for one in sys.stdin:
break
int(one)
for two in sys.stdin:
break
int(two)
three = 0
print("Output:")
three = one + two
sys.stdout.write(three)
我得到的输出是:
Inputs:
1
2
Output:
1
2
预期输出为 3
。但是我得到的是在上面的输出中显示的。
我尝试使用相同的代码 input()
:
one = int(input())
two = int(input())
three = one + two
print(three)
我得到的输出是 3
。我的第一个代码中缺少什么?
我想你想做的是:
aNumber = input('Enter a number: ')
anotherNumber = input('Enter another number: ')
print(int(aNumber) + int(anotherNumber))
要使用 stdin/out 执行此操作,您可以使用:
import sys
print("Inputs:")
one = sys.stdin.readline()
two = sys.stdin.readline()
print("Output:")
three = int(one) + int(two)
four = str(three)
sys.stdout.write(four)