添加两个变量

时间:2015-12-08 02:31:33

标签: 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? ")
    numbertwo = input("What is your second number? ")
    print ("Your equation is ",numberone, "+ ",numbertwo, )
    answer = (numberone * numbertwo)
    print ("Your answer is ",answer, )

当我打印答案时,它会以两个数字组合出来。例如,如果我使用的是9 + 10,它将以910的形式出现。我不知道如何解决它。

1 个答案:

答案 0 :(得分:0)

input获取用户的输入并将其作为字符串返回。

您需要将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, )