Python 添加两个变量
我在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? ")
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。