Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
PLUS
Dave Harker
Courses Plus Student 15,510 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