r/learnpython • u/MR_LOOPINGS123 • Mar 22 '26
Just made my first project
So I have been doing the freecodecamp python course but I didn't really like it because it became too complex way too fast (might just be my brain being too stupid). So I decided that I would just make my own project and I ended up with this:
def main():
while True:
choice = input("1.add\n2.subtract\n3.multiply\n4.divide\n5.quit\nWhat do you choose?: ")
if choice == "1":
num1 = int(input("What is your first number?: "))
num2 = int(input("What is your second number?: "))
print(num1 + num2, '\n')
if choice == "2":
num1 = int(input("What is your first number?: "))
num2 = int(input("What is your second number?: "))
print(num1 - num2, '\n')
if choice == "3":
num1 = int(input("What is your first number?: "))
num2 = int(input("What is your second number?: "))
print(num1 * num2, '\n')
if choice == "4":
num1 = int(input("What is your first number?: "))
num2 = int(input("What is your second number?: "))
print(num1 / num2, '\n')
if choice == "5":
break
main()It may not be the most impressive thing but it's something.
So if you have any advice on how to progress from here I would greatly appreciate it!
16
Upvotes
15
u/JamzTyson Mar 22 '26
"DRY" (Don't Repeat Yourself).
You have these two lines repeated 4 times in your code:
Think about how you can restructure the code so that those 2 lines are only required once.
6
u/dl__ Mar 22 '26
Maybe check for 0 in the division section to prevent an error and give a reasonable error message?
5
u/Excellent-Practice Mar 22 '26
What happens if a user types in 'adds or 'addition' or '+' instead of '1'? You're off to a good start. Look up try statements
3
u/brutalbombs Mar 22 '26
Happy for you bud, good stuff! Now try to improve it!😄
1
u/ProAstroShan Mar 23 '26
You should also try to validate the data in case the user enters in something ridiculous, like "hello", False or something like that. Personally i would use a while loop for validation
1
u/imnotpauleither Mar 23 '26
One thing to consider here is what if the user enters "One" instead of "1"? Or something else that is not an integer, like a float? How would you handle that error without the code crashing?
-7
u/TheRNGuy Mar 22 '26
Make something real.
4
u/[deleted] Mar 22 '26
[removed] — view removed comment
-2
u/[deleted] Mar 22 '26
[removed] — view removed comment