How do I have a Python 3 program check if changing a variable's type would cause a crash before trying to change the type?
Question:
I’m making a custom subroutine called userinput() in order to replace input().
-
I haven’t tried anything as I have no clue what to try.
-
The program crashes when I try to convert the input from a string to an int/float if the input is not a number.
-
Here’s the function:
def userinput(text,valan,valtype):
if valan == 0 and valtype == 0:
a = input(text)
elif valan == 0 and valtype != 0:
val = 0
while val == 0:
a = input(text)
if valtype == "int":
a = int(a)
elif valtype == "float":
a = float(a)
if type(a) != valtype:
print("Invalid answer.")
time.sleep(1)
print("Try again.")
time.sleep(1)
else:
val = 1
elif valan != 0 and valtype == 0:
val = 0
while val == 0:
a = input(text)
for i in range(0,len(valan)):
if valan[i] == a:
val = 1
if val == 0:
print("Invalid answer.")
time.sleep(1)
print("Try again.")
time.sleep(1)
return a
Answers:
I am not sure i understood correctly what you are trying to archieve with your function but you can try to catch the exception (ValueError) that python throws when trying to cast your variable to a different datatype.
The problem here might be those instructions like a = int(a)
or a = float(a)
. You may try to replace them with:
try:
a = int(a)
except ValueError:
print("Int conversion Failed")
and
try:
a = float(a)
except ValueError:
print("Float conversion Failed")
Apart from that, it is not clear in your code for example if the valtype
parameter should be an integer or a string (this may cause other errors). If you can give a more detailed explanation of your problem, maybe it’s easier also for other people to get in touch with you for helping.
Try/Catch documentation for further reading.
I’m making a custom subroutine called userinput() in order to replace input().
-
I haven’t tried anything as I have no clue what to try.
-
The program crashes when I try to convert the input from a string to an int/float if the input is not a number.
-
Here’s the function:
def userinput(text,valan,valtype): if valan == 0 and valtype == 0: a = input(text) elif valan == 0 and valtype != 0: val = 0 while val == 0: a = input(text) if valtype == "int": a = int(a) elif valtype == "float": a = float(a) if type(a) != valtype: print("Invalid answer.") time.sleep(1) print("Try again.") time.sleep(1) else: val = 1 elif valan != 0 and valtype == 0: val = 0 while val == 0: a = input(text) for i in range(0,len(valan)): if valan[i] == a: val = 1 if val == 0: print("Invalid answer.") time.sleep(1) print("Try again.") time.sleep(1) return a
I am not sure i understood correctly what you are trying to archieve with your function but you can try to catch the exception (ValueError) that python throws when trying to cast your variable to a different datatype.
The problem here might be those instructions like a = int(a)
or a = float(a)
. You may try to replace them with:
try:
a = int(a)
except ValueError:
print("Int conversion Failed")
and
try:
a = float(a)
except ValueError:
print("Float conversion Failed")
Apart from that, it is not clear in your code for example if the valtype
parameter should be an integer or a string (this may cause other errors). If you can give a more detailed explanation of your problem, maybe it’s easier also for other people to get in touch with you for helping.
Try/Catch documentation for further reading.