简体   繁体   English

计算器python题

[英]Calculator in python problems

I made a calculator in python but when I run it and do for example 123 and 321 I get 123321 instead of 444, What am I doing wrong?我在 python 中制作了一个计算器,但是当我运行它并执行例如 123 和 321 时,我得到的是 123321 而不是 444,我做错了什么?

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)

input() returns string not number . input()返回 string 而不是 number 。 That's why instead of addition , String concatenation is performed.这就是为什么执行字符串连接而不是加法的原因。

you need to use int(x) and int(y) for conversion.您需要使用int(x)int(y)进行转换。

use this statement answear = int(x) + int(y)使用这个语句answear = int(x) + int(y)

input returns a string, and when you combine two strings the result is what you are seeing. input返回一个字符串,当您组合两个字符串时,结果就是您所看到的。

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

So you need to convert them to an integer, like this:因此,您需要将它们转换为整数,如下所示:

answear = int(x) + int(y)

你可以使用这个:

y=int(input())

This is because you are declaring it as a string.这是因为您将其声明为字符串。 Use a = int(input()) .使用a = int(input()) This will cast it into an integer. If you want to insert a decimal number use the float data type.这会将其转换为 integer。如果要插入十进制数,请使用float数据类型。

input() accepts and returns a string object and you need to typecast this into an integer (or float) if you want to perform arithmetic operations on it. input() 接受并返回一个字符串对象,如果要对其执行算术运算,则需要将其类型转换为整数(或浮点数)。 Performing a + operation on two strings merely concatenates them.对两个字符串执行 + 操作只是将它们连接起来。

Instead of input(), use int(input()).使用 int(input()) 代替 input()。 This will tell Python that the user is about to enter an integer.这将告诉 Python 用户即将输入一个整数。

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()

This is a simple problem to fix.这是一个要解决的简单问题。 When adding to integers or doing any other operation including an input and an int you need to do this:添加整数或执行任何其他操作(包括输入和整数)时,您需要执行以下操作:

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

so this put into your code looks like this:所以这放入你的代码看起来像这样:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM