I'm trying to add code onto this which allows me to check and give a response if i enter a negative in the active shell
Question:
print('How many cats do you have?')
numCats = input()
try:
if int(numCats) >= 4:
print('That is a lot of cats.')
else:
print('That is not that many cats.')
except ValueError:
print('You did not enter a number.')
def check_negative(s):
try:
if int(numCats) <=0
print('You cant have negative cats.')
else:
print('That is not that many cats.')
I get invalid syntax and im unsure what im doing wrong. Im pretty new to this sort of stuff so nay help is appreciated.
Answers:
There are a few syntax error in your code, and also you can just add negative check in the try-catch along with >= 4
check.
print('How many cats do you have?')
numCats = input()
try:
if int(numCats) <0:
print('You cant have negative cats.')
elif int(numCats) >= 4:
print('That is a lot of cats.')
else:
print('That is not that many cats.')
except ValueError:
print('You did not enter a number.')
def check_negatives():
if numCats < 0:
print("You cannot have negative cats")
numCats = int(input("How many cats do you have?n- "))
if numCats >= 4:
print("That is a lot of cats")
elif 4 > numCats > 0:
print("That is not many cats")
else:
check_negatives()
The numCats takes an integer input and the n adds a new line. It then checks if numCats is greater than or equal to 4, if so, then it prints "That is a lot of cats". If numCats if below 4, BUT also above 0 (so not negative) it prints "That is not many cats". Finally you don’t need the check_negatives function since being negative is the only other possibility, but I included it.
You can just leave it at
else:
print("You cannot have negative cats")
print('How many cats do you have?')
numCats = input()
try:
if int(numCats) >= 4:
print('That is a lot of cats.')
else:
print('That is not that many cats.')
except ValueError:
print('You did not enter a number.')
def check_negative(s):
try:
if int(numCats) <=0
print('You cant have negative cats.')
else:
print('That is not that many cats.')
I get invalid syntax and im unsure what im doing wrong. Im pretty new to this sort of stuff so nay help is appreciated.
There are a few syntax error in your code, and also you can just add negative check in the try-catch along with >= 4
check.
print('How many cats do you have?')
numCats = input()
try:
if int(numCats) <0:
print('You cant have negative cats.')
elif int(numCats) >= 4:
print('That is a lot of cats.')
else:
print('That is not that many cats.')
except ValueError:
print('You did not enter a number.')
def check_negatives():
if numCats < 0:
print("You cannot have negative cats")
numCats = int(input("How many cats do you have?n- "))
if numCats >= 4:
print("That is a lot of cats")
elif 4 > numCats > 0:
print("That is not many cats")
else:
check_negatives()
The numCats takes an integer input and the n adds a new line. It then checks if numCats is greater than or equal to 4, if so, then it prints "That is a lot of cats". If numCats if below 4, BUT also above 0 (so not negative) it prints "That is not many cats". Finally you don’t need the check_negatives function since being negative is the only other possibility, but I included it.
You can just leave it at
else:
print("You cannot have negative cats")