TypeError: not enough arguments for format string when using %s

前端 未结 2 1379
孤城傲影
孤城傲影 2021-01-02 07:32

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         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 08:30

    You need a set of parenthesis:

    >>> '%s %s' % 'a', 'b'  # what you have
    Traceback (most recent call last):
      File "", line 1, in 
    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))
    

提交回复
热议问题