How would I make it so I can give an error like "Answer not recognised" if the answer to my input is not of int
type?
I have already tried to incorporate things like .isdigit()
in my if
statement along with a few others.
I apologize for a large amount of code here - but I am required to post the entire function.
I have added a comment on top of the else statement to which I am referring to. (When trying .isdigit()
I changed to an elif
).
def rating_system():
rate = input(
"\nWould you like to rate your experience with MealMatcher? (y/n) ")
if rate in ["yes", "y", "yeah"]:
star = int(
input("How would you rate our service on a scale of 1 to 10? "))
if star in range(0, 11):
os.system(clear)
confirmation = input(
f"Thank you for your rating! | You gave our service {star} stars out of 10, is that correct? (y/n) ")
if confirmation in ["yes", "y", "yeah"]:
os.system(clear)
close = input(
"Thank you for using MealMatcher! Would you like to close? (y/n)")
if close in ["yes", "y", "yeah"]:
os.system(clear)
print(
"Closing Application... | Thank you for using MealMatcher" * 3)
raise SystemExit
elif close in ["no", "n", "nope"]:
os.system(clear)
print("Well you have to...")
print("*kicks out*")
print(
"\x1B[3m" "User in your channel was banned from the server." "\x1B[0m")
raise SystemExit
else:
os.system(clear)
print("Answer Invalid")
elif confirmation in ["no", "n", "nope"]:
os.system(clear)
print("Retrying Statement... \n")
rating_system()
else:
os.system(clear)
print("Answer Invalid")
rating_system()
elif star not in range(0, 11):
os.system(clear)
print("That is not a valid answer | Try again: \n")
rating_system()
# Referring to the below else statement:
else:
os.system(clear)
print("Answer not Recognised.")
rating_system()
elif rate in ["no", "n", "nope"]:
os.system(clear)
print("Closing Application | Thank you for your time. \n" * 3)
raise SystemExit
else:
os.system(clear)
print("Answer Invalid \n")
rating_system()
CodePudding user response:
Base on your code to check if star
is int
modifiy your code as follow:
try:
star = int(input("How would you rate our service on a scale of 1 to 10? "))
except ValueError:
print("Invalid input, please use numbers only!")
# re-reun your function
CodePudding user response:
You could do it like this
inp = input("Enter input: ")
if type(inp) != int:
print("Answer not recognized")