I want to get only positive numbers in python .
and if the user enter a negative number the program show an error message .
should I code lots of conditions like
if this : do this
if that : do that
and so on .
or is there another way for this ?
I want to get only positive numbers in python .
and if the user enter a negative number the program show an error message .
should I code lots of conditions like
if this : do this
if that : do that
and so on .
or is there another way for this ?
 
    
     
    
    Use abs(x) Python native function.
EDIT: if you want to raise an error, instead, I would make use of assertions (example taken from here:
x = 0
assert x > 0, 'Only positive numbers are allowed'
print('x is a positive number.')
 
    
    You can use just an if statement like the code below:
n = float(input("Enter a positive number: "))
if num <= 0:
    raise ValueError("Please enter only positive numbers")
If the number is not a positive one you can manage as you prefer. In the example I put a raise, but you can also use a print statement with the message or an assertion.
 
    
    Not sur to understand your question,
you only need to do if x >= 0: if you want to check if x is positive or negative
 
    
    Simple options to achieve this can be
