Python_HD
Python_HD

Reputation: 3

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

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?

Upvotes: 0

Views: 1462

Answers (2)

Ashish M J
Ashish M J

Reputation: 700

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

Upvotes: 1

Orbital
Orbital

Reputation: 583

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.

Upvotes: 1

Related Questions