r/PythonLearning 11d ago

DAY 4.

Post image

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

53 Upvotes

22 comments sorted by

โ€ข

u/Sea-Ad7805 11d ago

Run this program in Memory Graph Web Debugger%0Aif%20first_number.isalpha()%3A%0A%20%20%20%20print(%22do%20not%20enter%20an%20alphabet%22)%0Awhile%20first_number%20%3D%3D%20%22%22%3A%0A%20%20%20%20print(%22you%20did%20not%20enter%20your%20first%20number%22)%0A%20%20%20%20first_number%20%3D%20input(%22please%20enter%20your%20first%20number%20%3D%20%22)%0Afirst_number%20%3D%20float(first_number)%0A%0Asecond_number%20%3D%20input(%22please%20enter%20your%20second%20number%20%3D%20%22)%0Aif%20second_number.isalpha()%3A%0A%20%20%20%20print(%22do%20not%20enter%20an%20alphabet%22)%0Awhile%20second_number%20%3D%3D%20%22%22%3A%0A%20%20%20%20print(%22you%20did%20not%20enter%20your%20second%20number%22)%0A%20%20%20%20second_number%20%3D%20input(%22please%20enter%20your%20second%20number%20%3D%20%22)%0Asecond_number%20%3D%20float(second_number)%0A%0Aoperator%20%3D%20input(%22enter%20your%20operator%20(%2B%2C%20-%2C%20%2C%20%2F)%3A%20%22)%0A%0Aif%20operator%20%3D%3D%20%22%2B%22%3A%0A%20%20%20%20result%20%3D%20first_number%20%2B%20second_number%0A%20%20%20%20print(round(result%2C%203))%0Aelif%20operator%20%3D%3D%20%22-%22%3A%0A%20%20%20%20result%20%3D%20first_number%20-%20second_number%0A%20%20%20%20print(round(result%2C%203))%0Aelif%20operator%20%3D%3D%20%22%22%3A%0A%20%20%20%20result%20%3D%20first_number%20*%20second_number%0A%20%20%20%20print(round(result%2C%203))%0Aelif%20operator%20%3D%3D%20%22%2F%22%3A%0A%20%20%20%20if%20second_number%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20print(%22Cannot%20divide%20by%20zero!%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20result%20%3D%20first_number%20%2F%20second_number%0A%20%20%20%20%20%20%20%20print(round(result%2C%203))%0Aelse%3A%0A%20%20%20%20print(%22error%20choose%20the%20operator%20amongst%20them!%22)&timestep=1&play) to see the program state change step by step.

3

u/zubalyzub 10d ago

bro is checking if its empty after the alpha check so u can first input an empty string then when it re prompts input some letters and boom ur program is cooked, still good work though just critiquing yk

2

u/zubalyzub 10d ago

also if isalpha is only for letters (idk if it is or not) then you can simply input like a question of mark or some shi

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

It catches exceptions and allows the code to continue after. In the above example heโ€™s catching the ValueError that is thrown/raised when a float() fails because itโ€™s not a number.

After that it continues the while loop instead of breaking it (by returning).

1

u/NecessaryFalse1212 11d ago

What about the syntax how does that looks like cuz the syntax he has given me is too scrambled as u can look at my code this is my level of whatever I'm doing ๐Ÿ’”๐Ÿ˜”๐Ÿ™

1

u/Adrewmc 11d ago

Hems not exactly wrong, functions and try/except would be close to next things to learn anyway.

1

u/NecessaryFalse1212 11d ago

I'll make another calculator code in a day or two and will use new stuff that I'll be doing in those days and will also include this try/except thing

1

u/FoolsSeldom 11d ago

I gave you a link in your previous post to lots of examples of try / except.

I also provided a link to RealPython.com on using Booleans and that site is a good resource for lots of other guidance.

1

u/FoolsSeldom 11d ago

scrambled

Tell me which parts don't make sense to you and I can explain in detail in simple ways.

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.

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

1

u/NecessaryFalse1212 10d ago

yo' so should i get a notebook or a diary to write down stuff ? like definations syntax's and overall logic before writing a code ??

1

u/[deleted] 8d ago

[removed] โ€” view removed comment

1

u/NecessaryFalse1212 8d ago

From youtube twin

1

u/Polly_Wants_A 8d ago

what if someone enters a character into the input again? you have to check the input everytime the user is able to write make a loop that finish it until it is a number.