Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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
Python 添加两个变量_Python_Python 3.x - Fatal编程技术网

Python 添加两个变量

Python 添加两个变量,python,python-3.x,Python,Python 3.x,我在Python中遇到了一个问题。我想做一个加法计算器,但突然出现了一个问题。附上我的密码 prompt = input("Do you want to use this calculator? Y for yes and N for no ") if prompt == 'n' : print ("Maybe next time. ") if prompt == 'y': numberone = input("What is your first number? ")

我在Python中遇到了一个问题。我想做一个加法计算器,但突然出现了一个问题。附上我的密码

prompt = input("Do you want to use this calculator? Y for yes and N for no ")
if prompt == 'n' :
    print ("Maybe next time. ")
if prompt == 'y':
    numberone = input("What is your first number? ")
    numbertwo = input("What is your second number? ")
    print ("Your equation is ",numberone, "+ ",numbertwo, )
    answer = (numberone * numbertwo)
    print ("Your answer is ",answer, )

当我打印答案时,它是两个数字的组合。例如,如果我使用9+10,结果将是910。我不知道如何修复它。

input
从用户那里获取输入并将其作为字符串返回

您需要将
输入
转换为数字。我假定您希望数字为
int
,但如果需要,您可以将
int
替换为
float

prompt = input("Do you want to use this calculator? Y for yes and N for no ")
if prompt == 'n' :
    print ("Maybe next time. ")
elif prompt == 'y':
    numberone = int(input("What is your first number? "))
    numbertwo = int(input("What is your second number? "))
    print ("Your equation is ",numberone, "+ ",numbertwo, )
    answer = (numberone + numbertwo)
    print ("Your answer is ",answer, )

顺便说一句,
n+m
不是一个等式,它是一个表达式。:)谢谢,我将更改它。另一方面,您的代码将两个数字相乘,而不是相加。感谢您为我注意到这一点。@erip——不是下行表决器,而是旁注:在Python 2中,
input
将计算您输入的值,因此您无需将其转换为
int
;但是,它实际上与
eval(原始输入(…)
)相同,这就是为什么要读取
str
时使用
raw\u输入
,否则可能会出现
名称错误
。在Python3中,情况并非如此,
input
具有与
raw\u input
相同的行为(并且
raw\u input
不存在)。也许上面的标记需要更改为
python-3.x
。如果它对您有效,您可以通过单击复选标记选择它作为答案。至于在哪里学习python。