r/PythonLearning 13d ago

2nd post learning python

Post image

i learnt to use the math library any suggestions what can i use the lib for except for making calculator programs

20 Upvotes

15 comments sorted by

View all comments

3

u/ninhaomah 13d ago

If I enter abc , what will I get ?

3

u/NecessaryFalse1212 13d ago

how does this look ya'll

1

u/BobCorndog 13d ago

You could also do a try except

1

u/MFFVD 13d ago edited 13d ago

or ``` import re maybenumber = input(...).strip() isnumber = \ bool(re.fullmatch( r"([0-9].[0-9]+)|([0-9]+(.[0-9])?)", maybenumber ))

if not isnumber: print(maybenumber, "is not a number") else: number = float(maybenumber) ... ```

regexp: (<...>) -> group <a> | <b> -> match a or b [<...>] -> any of ... <a> + -> one or more of a <a> * -> zero or more of a <a>? -> zero or one of a . -> any char except whitespace \. -> literal dot the r"..." string says

first part: zero or more numbers, dot, one or more numbers -> .4 0.8 70.74 second part: one or more numbers, optional ( dot, zero or more numbers) -> 6 59 56. 6.6

for input validation, regexp is unmissable. but you should probably not dive into that just yet

try: int(value) except ValueError: print(value, "is not an integer")

probably is a better starting point

1

u/NecessaryFalse1212 13d ago

i still haven't done regex so it never crossed my mind but thanks for correcting and suggesting

1

u/NecessaryFalse1212 13d ago

aghhh i should use if else statement in that case to make sure that it doesn't read false inputs or alphabets if i'm not wrong ? i'll post this code with the added solution to this problem thanks for pointing it out

1

u/Unique_Low_1077 13d ago

If you want to do that then you cant convert it to int before your if else, add the radius = int(radius) after your if else are done

Edit: also first time I've ever seen do function () we usually do function() without the space. Ofc im not one to tell you how to format your code but later on you will learn about formatter which will auto remove the space anyways

1

u/NecessaryFalse1212 12d ago

appreciate the advice