Hi everyone!
Trying to make a simple code to handle exceptions. The task is to make a piece of code that checks if an inputted number is between a specified range (in this case -10 to 10). I can make it handle an exception if it falls out of the range, no problem. But I also need a ValueError exception if somebody inputs non numeric values. But I can’t make the ValueError exception work for some reason. Here’s the code:
def read_int(prompt, min, max): try: if prompt in range(min, max): return str(prompt) if prompt not in range(min, max): raise IndexError except IndexError: return “Error: Value not in range ” + str(min) + “, ” + str(max) except ValueError: return “Error: Wrong input” v = read_int(int(input(“Enter a number from -10 to 10: “)), -10, 10) print(“The number is:”, v)
v = read_int(int(input(“Enter a number from -10 to 10: “)), -10, 10)
print(“The number is:”, v)
I’m certain that it’s something REALLY basic that I’m missing here, but I can’t figure it out for the life of me. Please help!!
submitted by /u/benmilesrocks
[link] [comments]
r/learnpython Hi everyone! Trying to make a simple code to handle exceptions. The task is to make a piece of code that checks if an inputted number is between a specified range (in this case -10 to 10). I can make it handle an exception if it falls out of the range, no problem. But I also need a ValueError exception if somebody inputs non numeric values. But I can’t make the ValueError exception work for some reason. Here’s the code: def read_int(prompt, min, max): try: if prompt in range(min, max): return str(prompt) if prompt not in range(min, max): raise IndexError except IndexError: return “Error: Value not in range ” + str(min) + “, ” + str(max) except ValueError: return “Error: Wrong input” v = read_int(int(input(“Enter a number from -10 to 10: “)), -10, 10) print(“The number is:”, v) v = read_int(int(input(“Enter a number from -10 to 10: “)), -10, 10) print(“The number is:”, v) I’m certain that it’s something REALLY basic that I’m missing here, but I can’t figure it out for the life of me. Please help!! submitted by /u/benmilesrocks [link] [comments]
Hi everyone!
Trying to make a simple code to handle exceptions. The task is to make a piece of code that checks if an inputted number is between a specified range (in this case -10 to 10). I can make it handle an exception if it falls out of the range, no problem. But I also need a ValueError exception if somebody inputs non numeric values. But I can’t make the ValueError exception work for some reason. Here’s the code:
def read_int(prompt, min, max): try: if prompt in range(min, max): return str(prompt) if prompt not in range(min, max): raise IndexError except IndexError: return “Error: Value not in range ” + str(min) + “, ” + str(max) except ValueError: return “Error: Wrong input” v = read_int(int(input(“Enter a number from -10 to 10: “)), -10, 10) print(“The number is:”, v)
v = read_int(int(input(“Enter a number from -10 to 10: “)), -10, 10)
print(“The number is:”, v)
I’m certain that it’s something REALLY basic that I’m missing here, but I can’t figure it out for the life of me. Please help!!
submitted by /u/benmilesrocks
[link] [comments]