1

For some reason the console only displays the first letter of what the reader inputs. Why is this happening?

Here is the code:

print ('Welcome to MadLibs')
string1 = input('Noun')
string2 = input('Plural Noun')
string3 = input('Noun')
string4 = input('Place')
string5 = input('Adjective')
string6 = input('Noun')
string='Be kind to your {}-footed {} For a duck may be somebody\'s {}, Be kind to your {} in {} Where the weather is always {}. You may think that this is the {}, Well it is.'
print(string.format(*string1,string2,string3,string4,string5,string6))
halfelf
  • 9,737
  • 13
  • 54
  • 63
clinggi5
  • 19
  • 4

2 Answers2

0

You have an asterisk (*) at the very beginning of your format call that you need to remove:

print(string.format(string1,string2,string3,string4,string5,string6))

The asterisk is how you unpack arguments to a function, something you don't need in this case. You also have 7 placeholders in your string, but only pass 6 variables into the format call. If you're using Python 3.6 and later, you should use f-strings instead (they're really great):

print(f"Be kind to your {string1}-footed {string2} For a duck "
      f"may be somebody's {string3}, Be kind to your {string4} "
      f"in {string5} Where the weather is always {string6}. "
      f"You may think that this is the {string7}, Well it is.")

F-strings make this kind of error a lot harder to introduce.

Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74
  • Removing the * only gives me:Traceback (most recent call last): File "python", line 9, in IndexError: tuple index out of range – clinggi5 Nov 08 '18 at 01:56
0

The * before string1 spreads the elements of that string into separate arguments. So it's equivalent to:

print(string.format(string1[0], string1[1], string1[2], ...,string2,string3,string4,string5,string6))

See What does ** (double star/asterisk) and * (star/asterisk) do for parameters? for the meaning of * in function definitions and argument lists.

Get rid of that.

print(string.format(string1,string2,string3,string4,string5,string6))

But now you don't have enough strings for all the placeholders in the format string. You're missing the Noun for Be kind to your {} in. So it should be:

print ('Welcome to MadLibs')
string1 = input('Noun')
string2 = input('Plural Noun')
string3 = input('Noun')
string4 = input('Noun')
string5 = input('Place')
string6 = input('Adjective')
string7 = input('Noun')
string='Be kind to your {}-footed {} For a duck may be somebody\'s {}, Be kind to your {} in {} Where the weather is always {}. You may think that this is the {}, Well it is.'
print(string.format(string1,string2,string3,string4,string5,string6,string7))
Barmar
  • 741,623
  • 53
  • 500
  • 612