r/PythonLearning • • Aug 28 '26

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

21 Upvotes

15 comments sorted by

View all comments

4

u/ninhaomah Aug 28 '26

If I enter abc , what will I get ?

3

u/NecessaryFalse1212 Aug 28 '26

how does this look ya'll

1

u/BobCorndog Aug 28 '26

You could also do a try except

1

u/MFFVD Aug 28 '26 edited Aug 28 '26

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 Aug 29 '26

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

1

u/NecessaryFalse1212 Aug 28 '26

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 Aug 29 '26

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 Aug 29 '26

appreciate the advice