类型错误:使用 %s 时没有足够的格式字符串参数 [英] TypeError: not enough arguments for format string when using %s

查看:42
本文介绍了类型错误:使用 %s 时没有足够的格式字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

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.

推荐答案

您需要将字符串格式的参数放在括号中:

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

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

否则,Python 只会将 name 视为字符串格式化的参数,而将其余部分视为 print 函数的参数.

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

但是请注意,现在不赞成使用 % 进行字符串格式化操作.现代方法是使用 str.format:

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))

这篇关于类型错误:使用 %s 时没有足够的格式字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆