Python Python Basics Types and Branching Numbers

so i write this program to calculate x+y but didn't work

this is the code x = input(" Enter the first number : " ) y = input(" Enter the secound number : ") float(x) float(y) print("The result : ", x + y)

1 Answer

Dave Harker
PRO
Dave Harker
Pro Student 15,508 Points

Hi ,

The float(x) and float(y) are not assigning the cast x and y string values to anything.

You can either cast them into the same variable name - altering type from cast
(or another one if you prefer)

x = input(" Enter the first number : " )
y = input(" Enter the second number : ")

x = float(x)
y = float(y)

print("The result : ", x + y)

Or cast them in the print statement

x = input(" Enter the first number : " )
y = input(" Enter the second number : ")

print("The result : ", float(x) + float(y))

Or cast them in the input request if you really wanted to (makes it harder to read I think though)

x = (float)(input(" Enter the first number : " ))
y = (float)(input(" Enter the second number : "))

print("The result : ", x + y)

Whatever you like really! Best of luck, and happy coding :smile:
Dave :dizzy:

thanks you so much