13

this is my code

import sys
name = input("Enter your name:")
last_name = input("Enter your last name:")
gender = input("Enter your gender:")
age = input("Enter your age:")
print ("So your name is %s, your last name is %s, you are %s and you are %s years old" % name, last_name, gender, age)

I've searched the topic but I don't understand.

2 Answers 2

23

You need to put your arguments for string formatting in parenthesis:

print (... % (name, last_name, gender, age))

Otherwise, Python will only see name as an argument for string formatting and the rest as arguments for the print function.


Note however that using % for string formatting operations is frowned upon these days. The modern approach is to use str.format:

print ("So your name is {}, your last name is {}, you are {} and you are {} years old".format(name, last_name, gender, age))
Sign up to request clarification or add additional context in comments.

1 Comment

It's a good choice, to leave behind the old string format method from Python, and use this approach :). It avoids a lot of headaches and gives you more flexibility when it comes to better format the output, putting blank spaces in order to form columns of data, for example. Here is the official doc, which has plenty of examples :) -> pyformat.info
9

You need a set of parenthesis:

>>> '%s %s' % 'a', 'b'  # what you have
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>>
>>>
>>> '%s %s' % ('a', 'b')  # correct solution
'a b'

'%s %s' % 'a', 'b' is evaluated as ('%s %s' % 'a'), 'b', which produces an error in '%s %s' % 'a' since you have fewer arguments than format specifiers.


print("So your name is %s, your last name is %s, you are %s and you are %s years old" % (name, last_name, gender, age))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.