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

Please can you help me with this python calculator?

Hi, this calculator I'm doing doesn't return any results after the user inputs everything, https://w.trhou.se/a28naegx3z

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hi Andy, I see two issues with your y.py calculator:

  • The choice returned from input() in python 3 is a string. Change the comparisons to if choice == "1":, etc.
  • the 4th choice is indented under choice 3. Since choice can not be both 3 and 4, the division operation is not reachable.

Correcting:

if choice == '1':
  print(num1,"+",num2,"=",add(num1,num2))

if choice == '2':
  print(num1,"-",num2,"=",minus(num1,num2))

if choice == '3':
  print(num1,"X",num2,"=",multiply(num1,num2))

if choice == '4':
  print(num1,"/",num2,"=",divide(num1,num2))
else:
  print("invallid")

Hey Chris, thanks for that, it was a bit of a silly mistake also it also had the input set as a string not an in on both number variables but it now works apart from the fact it prints invalid below the returned sum please can you help me with this?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Each of the if statements are being executed every time. So when the choice is not 4 the "invalid" will always print.

Change the "ifs" to "elif" for an "else if" flow.