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!
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

hat eaters
Courses Plus Student 358 PointsPython Calculator program help!!!
Hi, I am a very new python programmer. I decided to make this simple calculator and after just adding some new features and working on it the use a new number function is not working anymore. I would love feedback about the program Thank you so much
import sys import math print("---- WELCOME TO CALCULATOR!----")
counter2 = -1 counter1 = 0 counter3 = 1 while counter1 == 0:
try:
num1 = float(input("Enter a number: \n"))
except ValueError:
print("Oops! Invalid number...")
else:
print("Valid number!\n")
counter2 += 1
while counter3 == 1:
operation = str(input(
'Pick option: \n A)Addition \n S)Subtraction \n D)Division \n M)Multiplication \n R)Root \n'
' E)Exponent \n P)Percantage \n'))
if operation == "A":
print("Valid input!")
counter2 = 0
counter3 = 0
elif operation == "S":
print("Valid input!")
counter2 = 0
counter3 = 0
elif operation == "D":
print("Valid input!")
counter2 = 0
counter3 = 0
elif operation == "M":
print("Valid input!")
counter2 = 0
counter3 = 0
elif operation == "R":
print("Valid input!")
counter2 = 0
counter3 = 0
elif operation == "E":
print("Valid input")
counter2 = 0
counter3 = 0
elif operation == "P":
print("Valid input")
counter2 = 0
counter3 = 0
else:
counter2 = 1
while counter2 == 0:
if counter3 == 0:
try:
num2 = float(input("Enter another number: \n"))
except ValueError:
print("Oops! Invalid number...")
counter2 = 0
else:
print("Valid number!\n")
counter2 = 1
while counter2 == 1:
if operation == "A":
result = num1 + num2
print("{} + {} = {}".format(num1, num2, round(result, 14)))
counter2 += 1
elif operation == "S":
result = num1 + num2
print("{} - {} = {}".format(num1, num2, round(result, 14)))
counter2 += 1
elif operation == "D":
result = num1 / num2
print("{} / {} = {}".format(num1, num2, round(result, 14)))
counter2 += 1
elif operation == "M":
result = num1 * num2
print("{} * {} = {}".format(num1, num2, round(result, 14)))
counter2 += 1
elif operation == "R":
result = num1 ** (1/num2)
print("The {} root of {} = {}".format(num2, num1, round(result, 14)))
counter2 += 1
elif operation == "E":
result = num1 ** num2
print("{} to the power of {} = {}".format(num1, num2, round(result, 14)))
counter2 += 1
elif operation == "P":
result = num1 / 100 * num2
print("{}% of {} = {}".format(num1, num2, rount(result, 14)))
counter2 += 1
else:
print("Invalid! check if the letter you entered is a capital")
while counter2 == 2:
try:
options = int(input("Choose option: \n 1)leave \n 2)Use a new number \n 3)Do a new equation \n"))
except ValueError:
print("Oops! Invalid number...")
else:
if options == 2:
print("Alright")
try:
num2 = float(input("Enter the new number you want to use : \n"))
except ValueError:
print("Oops! Invalid number...")
else:
print("Valid number!\n")
counter2 = 0
counter1 = 2
counter3 = 1
num1 = round(result, 14)
elif options == 3:
print("Alright")
counter1 = 0
counter2 = -1
counter3 = 1
else:
sys.exit()
2 Answers

Shane Robinson
7,324 PointsOh wow, that is a scary amount of nested ifs and while loops. If it works, I applaud you. :p I would recommend looking into switch statements if you want to clean up the code - it won't really affect performance but it should make the code look quite a bit more readable.
Edit: Scratch that about switch statements, I did not realize they didn't exist in Python. With that being the case it looks like a dictionary might be your next best option.

Christopher Shaw
Python Web Development Techdegree Graduate 58,236 PointsGood for you to get it to work. It is not very 'pythonic' and as you learn more you will be able to write more readable code by breaking your code down into functions.

hat eaters
Courses Plus Student 358 PointsThank you, I know its not the most readable code but I wanted to practice doing try, except and else and while loops so i could get a better grasp of them hopefully I will be able to fix the problem I am having with it soon

hat eaters
Courses Plus Student 358 PointsCan you please give me an example of how i can have a similar outcome if i use functions in parts of it. Thank you so much for the help
hat eaters
Courses Plus Student 358 Pointshat eaters
Courses Plus Student 358 PointsThanks for the feedback, I will see how I can use dictionaries to make it cleaner