有没有一种方法可以为一个输入框分配多个变量-Python [英] Is there a way to assign multiple variables for one input box - Python
问题描述
如何将多个变量分配给一个GUI输入框?像这样:q1,q2,q3 = input()
How would I assign multiple variables to one GUI input box? Something like this: q1, q2, q3 = input()
这不是代码的运行方式,但这只是我想要的样子:
This isn't how the code would go, but this is just something I want it to be like:
a, b, c = str(input("Type in a command"))
但不是这样:
abc = str(input("Type in a command"))
if abc == str("a"):
print ("a is right")
else:
print ("a is wrong")
if abc == str("b"):
print ("b is right")
else:
print ("b is wrong")
if abc == str("c"):
print ("c is right")
else:
print ("c is wrong")
如果我这样做的话,我会误解其中之一,它会告诉我一个是正确的&;2错了.(a是错误的,b是正确的,c是错误的)
If I did it this way, I'd get one of them wrong and it will tell me that one is right & 2 are wrong. (a is wrong, b is right, c is wrong)
推荐答案
input
只能返回一个字符串,但是您可以动态处理它:
input
can only return one string, but you can process it on the fly:
a, b, c = input('Type in a command').split()
如果输入中的单词"数量不同于3,则可能会导致 ValueError
,因此您可能要使用 try
-除外代码>来处理它.
This may result in ValueError
if the number of "words" in the input differs from 3, so you may want to use try
-except
to handle it.
try:
a, b, c = input('Type in a command').split()
except ValueError:
print('Invalid input. Please enter a, b and c')
这篇关于有没有一种方法可以为一个输入框分配多个变量-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!