0

Please check why my beginner's Calculator isn't working!

#Project "basic Calculator" #-I am a beginner, so dont judge plz import math #FIRST ALGORITHM THAT DIDNT WORK #----------------------------------------------------------------------- #x = float(input("Give me a number: ")) #print("Your first number is ", x) #y = float(input("Give me another number: ")) #print("Your second number is ", y) #z = str(input("Addition, Subtraction, Multiplication or Division ?")) #----------------------------------------------------------------------- #I SAW THIS TRICK ON STACKOVERFLOW BUT IT DIDNT WORK #----------------------------------------------------------------------- x , y = float(input("Give me two numbers:")).split(",") print("One number is ", x) print("Second number is ", y) z = str(input("Addition, Subtraction, Multiplication or Division ?")) #----------------------------------------------------------------------- #THE IF STATEMENTS #----------------------------------------------------------------------- if z == "Addition" or z == "addition": print(x + y) elif z == "Subtraction" or z == "subtraction": print(x - y) elif z == "Multiplication" or z == "multiplication": print(x * y) elif z == "Division" or z == "division": print(x / y) else: print("Sorry we couldn't catch you, can you repeat?") z = str(input("Addition, Subtraction, Multiplication or Division ?")) #-----------------------------------------------------------------------

7th Apr 2020, 12:52 PM
Angelos Karasavvidis
Angelos Karasavvidis - avatar
1 Answer
+ 2
This is because in the line x, y = float(input("Give me two numbers:")).split (",") .split() is a method for strings. float() converts the input to float and .split() cannot work on floats. So what you could've done was x, y = float(input("Give me two numbers:").split(",")) #the brackets have been changed. What it would do is split the input by a comma and change it to a list. So the input 12,13 will become ["12", "13"] But the problem is, it would still not work, because float() cannot have a list as arguement (that is, float (["12", "13"]) would give an error. So what you can do is first take the input separated by comma, and then convert each to a float. There are better ways (like map()), but this is good for a beginner. Like this x, y = input("Give me two numbers:").split(",") x = float(x) y = float(y) #the rest of the code EDIT: I didn't see that there are 2 ways in question. But as for me, the first way of taking input is working fine
7th Apr 2020, 1:16 PM
XXX
XXX - avatar