问题
So I was making a console application using Python 3.7; that heavily depends on input (wowz).
The application's function is to "swap" between two integer variables' values. And that is not where the problem is at, the problem is when I try to validate the user's input by checking the data-type for the input using a couple "if statements", and no matter what the user inputs using the "input()" function; the input's data-type will always be defined as ""
I just want this little piece of ART to run dynamically. (I want the input() function to dynamically detect the data-type of the input and assign it to the variable? plsss)
P.S.: I didn't try anything, since all I found was useless; I guess. (tells me to use the int() function, e.g.: {
# Works just fine when input is fully composed of integral numbers.
a = int(input("Enter an integer as a value for A: "))
} (EDIT OF Line 5 BELOW)
I don't want to use the int() function; since it'll cause a pile of problems, if the user's input was a string that was not fully composed of integral numbers. (ValueError)
def swap(x, y):
return y, x
aValid = 1
bValid = 1
a = input("Enter an integer as a value for A: ")
if (str(type(a)) != "<class 'int'>"):
print("Please take us seriously, we're watching you.\n")
aValid = 0
while (aValid == 0):
a = input("Enter an integer as a value for A: ")
if str(type(a)) != "<class 'int'>":
print("Please take us seriously, we're watching you.\n")
aValid = 0
else:
aValid = 1
# ! To be worked on:
b = input("Now, Please enter the value for B: ")
print("A = " , a)
print ("B = ", b)
a, b = swap(a, b)
print("Now:\nA = ", a)
print("B = ", b)
I expected the input() function in Python 3.7 (32bit) to dynamically detect the data-type of the input and assign it to the variable along with the input itself.
But what actually happens is that it always assigns the input's data-type as "< class 'str' >"; (no spaces after < and before >, ) which causes the program to go into an infinite loop, and it is giving me a headache; my stupidity.
回答1:
So, in python 2 the input()
function would detect the type of the user's input. However, in python3 input()
refers to raw_input()
in python 2. In order to dynamically detect the type, you can use the ast
, specifically ast.literal_eval
.
You could potentially write your own input function as follows:
import ast
def input_detect(s):
return ast.literal_eval(input(s))
and then your code would look like:
import ast
def input_detect(s):
return ast.literal_eval(input(s))
def swap(x, y):
return y, x
aValid = 1
bValid = 1
a = input_detect("Enter an integer as a value for A: ")
if (str(type(a)) != "<class 'int'>"):
print("Please take us seriously, we're watching you.\n")
aValid = 0
while (aValid == 0):
a = input_detect("Enter an integer as a value for A: ")
if str(type(a)) != "<class 'int'>":
print("Please take us seriously, we're watching you.\n")
aValid = 0
else:
aValid = 1
# ! To be worked on:
b = input_detect("Now, Please enter the value for B: ")
print("A = " , a)
print ("B = ", b)
a, b = swap(a, b)
print("Now:\nA = ", a)
print("B = ", b)
回答2:
In python 3 the input function is raw_input. It means that it always will be a string. But, if you'd want it to take a the format of its value, you should use the: ast.literal_eval() function. Be aware: don't use the eval() function! It's not safe as it can I delete files from the computer.
Also: don't use a swap function, it's unpythonic. x, y = y, x
回答3:
Perhaps a while
loop and a try/except
is a more reliable way of capturing and validating input. Also, we avoid the use of eval/literal_eval
, which can be problematic for security.
For example:
a = None
b = None
while a is None:
try:
a = int(input("Enter an integer as a value for A: "))
except ValueError:
continue
while b is None:
try:
b = int(input("Enter an integer as a value for B: "))
except ValueError:
continue
print("A is {}".format(a))
print("B is {}".format(b))
a, b = b, a
print("A is now {}".format(a))
print("B is now {}".format(b))
This way your code is more robust, because you don't have to account for different type checking, and if you were to write a validation function, say validate_user_input
, you could add a whole host of checks which raise a ValueError
is they fail, and your code would barely change.
来源:https://stackoverflow.com/questions/57013667/can-the-input-function-in-python-dynamically-detect-the-inputs-data-type