-1

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"))

But not like this:

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")

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)

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
GotRiff
  • 87
  • 1
  • 3
  • 9

4 Answers4

2

input can only return one string, but you can process it on the fly:

a, b, c = input('Type in a command').split()

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')
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
2

Input returns only a single string. You can store the input and then process it as per your needs. A simple and safe way to take multiple variable input is :

s = input().split()

Here, s gives you a list of your whitespace separated inputs. This can take in any number of options.

You can then process each individually :

for i in s :
    if i in ('a','b','c') : 
        print(i, " is right")
    else :
        print(i, " is wrong")
asheeshr
  • 4,088
  • 6
  • 31
  • 50
0

If you want to use different types, you could probably use ast.literal_eval:

a,b,c = ast.literal_eval("3,4,5")
a,b,c = ast.literal_eval("3,4.5,'foobar'")

This works because ast evalutes the string into a tuple which contains literals. It is then unpacked on the left hand side. Of course, for this to work, the elements must be separated by commas.

mgilson
  • 300,191
  • 65
  • 633
  • 696
0

I think the easiest way is to use

 a = b = c = str(input("Type in a command"))
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 05 '23 at 05:23