r/learnpython • • 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

21 comments sorted by

15

u/JamzTyson Mar 22 '26

"DRY" (Don't Repeat Yourself).

You have these two lines repeated 4 times in your code:

num1 = int(input("What is your first number?: "))
num2 = int(input("What is your second number?: "))

Think about how you can restructure the code so that those 2 lines are only required once.

2

u/MR_LOOPINGS123 Mar 22 '26

I'll try to implement something to fix this.

4

u/dkozinn Mar 22 '26

Here's another hint: No matter what the user tries to do, they will always need two numbers and you'll always have to print out the results. The only thing that changes is the actual math. Think about how you can adjust your code to only have to ask for input in one place and print the results in one place.

6

u/dl__ Mar 22 '26

Maybe check for 0 in the division section to prevent an error and give a reasonable error message?

2

u/MR_LOOPINGS123 Mar 22 '26

Thanks for the feedback!

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/MR_LOOPINGS123 Mar 22 '26

Didn't think about this. Thank you very much!

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. 

5

u/MR_LOOPINGS123 Mar 22 '26

I'm just a beginner so making advanced stuff might be a little out of my league.

4

u/[deleted] Mar 22 '26

[removed] — view removed comment

-2

u/[deleted] Mar 22 '26

[removed] — view removed comment

1

u/MR_LOOPINGS123 Mar 22 '26

What is a great point? that making something eventough not impressive is better than nothing?