python - 艰难地学习 Python:练习 19
问题描述
刚开始用 Zed Shaw 的书学习 python。在其中一个def function
想要使用raw_input
但不知道如何实现的练习中。任何帮助和建议表示赞赏。
运行代码时出现此错误:
File "drills19.py", line 27, in <module>
boys_and_girls(boys, girls)
File "drills19.py", line 2, in boys_and_girls
print "In your school there are %d boys." % boys_count
TypeError: %d format: a number is required, not str
问候,亚历克斯
def boys_and_girls(boys_count, girls_count):
print "In your school there are %d boys." % boys_count
print "In your school there are %d girs." % girls_count
print "Total number of students in the school is %d." % (boys_count + girls_count)
print "That's a lot of students!\n"
print "How many boys on the school?"
boys = raw_input(">")
print "How many girls in the school?"
girls = raw_input(">")
boys_and_girls(boys, girls)
解决方案
问题是raw_input
返回一个str
(即字符串),但%d
格式类型需要一个数字。您可以使用以下方法将其转换为数字int()
:
...
boys = int(raw_input(">"))
...
girls = int(raw_input(">"))
...
推荐阅读
- c# - 如何将错误响应发送回 OAuth2 客户端
- google-data-studio - 如何过滤 Google Data Studio 中的特定字段?
- arrays - 从标签数组创建到标签列表的链接
- node.js - 使用 $group(aggregation) 返回多个对象
- php - 为什么在 WIFI 下我可以看到 $_SESSION 变量而在 sim 网络下它们不存在?
- tensorflow - 具有离散值的 Keras Conv3D 层
- angular - 无法导航到Angular中的特定页面
- c++ - 使用 std::initializer_list[] 作为变长列表的 const 列表
- javascript - 如何在第二个下划线后剪切字符串?
- angular - 如何根据值或表单中没有值禁用 Angular 按钮