Python中的input()函数可以动态检测输入的数据类型吗? [英] Can the input() function in Python dynamically detect the input's data type?
问题描述
因此,我使用Python 3.7制作了控制台应用程序;很大程度上取决于输入(wowz).
应用程序的功能是在两个整数变量的值之间交换".那不是问题所在,问题在于,当我尝试通过使用几个"if语句"检查输入的数据类型来验证用户的输入时,无论用户使用" input()"功能进行输入;输入的数据类型将始终定义为"
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 ""
我只希望这小块ART动态运行.(我希望input()函数动态检测输入的数据类型并将其分配给变量?plsss)
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 .:我什么也没尝试,因为我发现所有的东西都是无用的.我猜.(告诉我使用 int()函数,例如:{
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: "))
} (下面第5行的编辑)
我不想使用 int()函数;如果用户输入的字符串不完全由整数组成,则可能会引起很多问题.(ValueError)
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)
我希望 Python 3.7(32位)中的 input()函数可以动态检测输入的数据类型并将其分配变量以及输入本身.
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.
但是实际上发生的是,它总是将输入的数据类型分配为<类'str'>" ;(<和>之前没有空格),这会使程序陷入无限循环,这让我很头疼;我的愚蠢.
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.
推荐答案
因此,在python 2中, input()
函数将检测用户输入的类型.但是,在python3中, input()
指的是python 2中的 raw_input()
.为了动态检测类型,可以使用 ast
,特别是 ast.literal_eval
.
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)
这篇关于Python中的input()函数可以动态检测输入的数据类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!