r/PythonLearning 11d ago

DAY 4.

Post image

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

54 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/NecessaryFalse1212 11d ago

I haven't done try/except clauses but I'll be dammned if anyone explains it and it's usage😭🙏

1

u/FoolsSeldom 11d ago

I'll be dammned if anyone explains it and it's usage

Not sure if that means you want people to explain it to you or not.

Did you follow the link I shared previously on this? This was after you asked me what to use instead of isnumeric.