在python问题的计算器

时间:2013-09-05 12:31:43

标签: python python-3.x

我在python中制作了一个计算器

import time
print("Calculator 1.0")
print("made by AnAwesomeMiner")
print("Number 1 in calculation")
x = input()
print("Number 2")
y = input()
print("calculating")
time.sleep(3)
print("why is this not done yet")
time.sleep(3)
print("god this is taking forever")
time.sleep(3)
print("done")
answear = x + y
print(answear)

但是当我运行它并做例如123和321时我得到的是123321而不是444,我做错了什么,顺便说一句,我不认为我是编程的新手

7 个答案:

答案 0 :(得分:7)

input()返回字符串而不是数字。这就是为什么不进行添加,执行字符串连接的原因。

您需要使用int(x)int(y)进行转换。

使用此声明answear = int(x) + int(y)

答案 1 :(得分:6)

input返回一个字符串,当你组合两个字符串时,结果就是你所看到的。

>>> x = '123'
>>> y = '321'
>>> x+y
'123321'

所以你需要将它们转换为整数,如下所示:

answear = int(x) + int(y)

答案 2 :(得分:2)

你可以用这个:

y=int(input())

答案 3 :(得分:0)

input()接受并返回一个字符串对象,如果要对其执行算术运算,则需要将其转换为整数(或浮点数)。对两个字符串执行+操作只是连接它们。

答案 4 :(得分:0)

使用int(input())而不是input()。这将告诉Python用户即将输入一个整数。

答案 5 :(得分:0)

def main():

    def add(x,y):
        return x + y
    def sub(x,y):
        return x - y
    def mult(x,y):
        return x * y
    def div(x,y):
        return x / y
    def remainder(x,y):
        return x % y
    repeat=True
    while repeat:
        select=int(input("please select any operation:-\n 1.ADD\n2.SUBTRACT\n3.MULTIPLY\n4.DIVIDE\n5.REMAINDER\nselect here:-"))
        num1=int(input("Enter the first number"))
        num2=int(input("Enter the second number"))

        if select==1:
            print(num1,"+",num2,"=",add(num1,num2))
        elif select==2:
            print(num1,"-",num2,"=",sub(num1,num2))
        elif select==3:
            print(num1,"*",num2,"=",mult(num1,num2))
        elif select==4:
            print(num1,"/",num2,"=",div(num1,num2))
        elif select==5:
            print(num1,"%",num2,"=",remainder(num1,num2))
        else:
            print("invalid input")
        print("Do you want to calculate further?\n press y for continue.\n press any other key to terminate.")
        repeat="y" in str(input())
        if repeat=="y":
            print("Ooo yeh! you want to continue")
        else:
            print("Tnakyou")
main()

答案 6 :(得分:0)

这是一个简单的问题。在将整数相加或执行任何其他操作(包括输入和int)时,您需要执行以下操作:

y = int(input())
x = int(input())
a = y+x

因此放入您代码中的代码如下:

import time
print("Calculator 1.0")
print("made by AnAwesomeMiner")
print("Number 1 in calculation")
x = int(input())
print("Number 2")
y = int(input())
print("calculating")
time.sleep(3)
print("why is this not done yet")
time.sleep(3)
print("god this is taking forever")
time.sleep(3)
print("done")
answear = x + y
print(answear)