[英]Math with direct input
I just started my first day at uni and there is one of the exercises I am already struggling with. 我刚从uni大学开始第一天的学习,而其中一项练习已经很艰苦。 This is the problem: Make code that inputs two given mumbers and output one minus the other
这是问题所在:使代码输入两个给定的成员,然后输出一个减去另一个
I did this: 我这样做:
number1 = int(raw_input("Type your first number: "))
number2 = int(raw_input("Type your second number: "))
result = number1 - number2
print result
But it was wrong because the input is direct so when the program tested my code it said: 但这是错误的,因为输入是直接的,所以当程序测试我的代码时,它说:
I have always done code that first asks for the information so I have no idea if this is even possible in python or how you do it. 我一直都在先要求信息的代码完成工作,所以我不知道这是否可能在python中实现或如何实现。 Any suggestions are appreciated, thanks.
任何建议表示赞赏,谢谢。
you need to use : 您需要使用:
number1, number2 = map(int, raw_input().split())
result = number1 - number2
print result
In the sample input both the numbers are separated by space, and there is no prompt for the user such as "Type your first number: "
, So all yo need to do is take input using raw_input()
, then splitting the input on " "
(space) by using .split()
and then converting each string after splitting to int
using the map
function. 在示例输入中,两个数字都用空格隔开,并且没有提示用户,例如
"Type your first number: "
,因此您要做的就是使用raw_input()
接受输入,然后将输入拆分为" "
(空格)通过使用.split()
然后将每个串转换分割后到int
使用map
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.