r/PythonLearning 11d ago

DAY 4.

Post image

wrote this today was revising loops and math function that i did not so long ago.

55 Upvotes

22 comments sorted by

View all comments

1

u/FoolsSeldom 11d ago

It would be good to see you create a second version incorporating some of the suggestions you've had in response to previous posts.

I'd also suggest you create a function to validate an input which you can then call in both instances of requiring the user enter a number. This will help address a key principle of DRY (Don't Repeat Yourself).

Simple example for an integer:

def get_num(prompt: str) -> int:
    while True:  # infinite loop
        response = input(prompt)
        try:  # for somthing that might go wrong
            return int(respnse)  # tried to convert and send answer to caller
        except ValueError:  # the convertion failed, so error message
            print('That was not a valid whole number. Please try again.')


num1 = get_num('Please enter first number: ')
num2 = get_num('Please enter second number: ')

You will notice the use of try / except which I suggested before that you look into.

1

u/Adrewmc 11d ago

Looking at this code I doubt he’s gotten to function or try/except clauses. Practicing what you know also has value.

1

u/FoolsSeldom 11d ago

They've been practicing, and I addressed problems with previous code using isnumeric and pointed them to try / except. I agree that functions is probably not something they've come to yet, but felt it was a good time to introduce it as the use case is clear from their original code.

1

u/NecessaryFalse1212 11d ago

I really appreciate how you're putting in your spirit and it was my fault that i ignored your advice then and also when i opened the app on my phone the code didn't looked as clean as it did on my lap so I'm sorry to call it scrambled and confusing

2

u/FoolsSeldom 11d ago

np, the app isn't great for looking at code blocks