Simply, I am entering a value, I want to determine whether the value is alpha or not. If it is not alpha, I want to check if it is a number or not. If it is a number I want to check if it is positive or negative.
I read a lot about checking a signed number like -50
. There are two ways, we can use something like this:
try:
val = int(x)
except ValueError:
print("That's not an int!")
Which I think I do not need it here and I do not know where to put it in my code.
The other way is to use .lstrip("-+")
, but it is not working.
amount = 0
while True:
amount = input("Enter your amount ===> ")
if amount.isalpha() or amount.isspace() or amount == "":
print("Please enter only a number without spaces")
elif amount.lstrip("-+").isdigit():
if int(amount) < 0:
print("You entered a negative number")
elif int(amount) > 6:
print("You entered a very large number")
else:
print(" Why I am always being printed ?? ")
else:
print("Do not enter alnum data")
What am I doing wrong ?
This is how you would integrate a try
/except
block:
amount = 0
while True:
amount = input("Hit me with your best num!")
try:
amount = int(amount)
if amount < 0:
print("That number is too tiny!")
elif amount > 6:
print("That number is yuge!")
else:
print("what a boring number, but I'll take it")
break # How you exit this loop
except ValueError:
print("Wow dude, that's like not even a number")
It does all the heavy lifting for you, as int()
can process numbers with +
/-
automatically.