0

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?

EternalObserver
  • 517
  • 7
  • 19
Python_HD
  • 3
  • 2
  • 1
    You are assigning Canada,New_york to a single value city. Hence the error –  May 29 '21 at 06:40

2 Answers2

1

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.

Orbital
  • 565
  • 3
  • 13
1

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 + "!!")
Ashish M J
  • 642
  • 1
  • 5
  • 11