Home > database >  I have an issue with my code in regard to returning a solution from my user input code and "str
I have an issue with my code in regard to returning a solution from my user input code and "str

Time:10-17

I am trying to add together two user inputs for numbers and show the results of the two added together. This is what my code currently looks like:

number1= eval(input("total price of cleaning for your house size:"))

number2= eval(input("Price for the cleaning type:"))

def addition(a,b): 
    addition =a-b

    return addition 

print("You owe:",addition(int(number1),int(number2)))

both user inputs are based on earlier calculations within my code. I had submitted a code almost identical to this, and it had performed correctly, so I am not sure why this one is not.

In the "assistant" it shows "str" is not callable, and redefining the name "addition" from the outer scope.

I have tried multiple different ways to make this code work and have not been able to have it run successfully to where it calculates and displays the cost.

CodePudding user response:

eval return int try this :

number1= eval(input("total price of cleaning for your house size:"))

number2= eval(input("Price for the cleaning type:"))

def addition(a,b): 
addition =a-b

return addition



print("You owe:",addition(number1,number2))

CodePudding user response:

I suspect you don't want to use eval, just:

number1= input("total price of cleaning for your house size:")

number2= input("Price for the cleaning type:")

def addition(a,b): 
    addition =a-b

    return addition 

print("You owe:",addition(int(number1),int(number2)))

input will present a prompt and read the value entered by the user into a string. eval will parse the expression the user enters and will try to execute it as a python script; the return value will be the results of evaluating the text as a python script. So, for example, x = 1; eval("x 1") will return 2.

I assume you just want to read an integer value, so:

number = int(input("Price for the cleaning type:"))
  • Related