Unable to input string values when taking multiple input in one line (Python)

1.4k Views Asked by At

Using the code below:

print("Welcome to band name generator!!")
city,pet = input("What is the name of the city you grew up in?\t") + input ("\nWhat is the name of your first pet?\t")
print("\n\t Your band name can be " + city + " "+ pet + "!!")

I can input single variable (eg - a/b/c or 1/2/3) and the program works fine but it we input string values or words(eg- Canada,New_york), I get the following error - too many values to unpack (expected 2)

How can I resolve this while keeping input in one line?

2

There are 2 best solutions below

0
On BEST ANSWER

You need to replace the + with a , since the + is going to concatenate your inputs into one string.

city, pet = input("What is the name of the city you grew up in?\t"), input ("\nWhat is the name of your first pet?\t")

Whenever you get the too many values to unpack Error, make sure that the number of variables on the left side matches the number of values on the right side.

0
On

Use the split function,it helps in getting a multiple inputs from user. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator.

print("Welcome to band name generator!!")
city,pet = input("Enter the city and pet name  ").split()
print("\n\t Your band name can be " + city + " "+ pet + "!!")