python - 为什么不能删除变量后面的空格?
问题描述
我似乎无法摆脱最后一行之间age1
的空间。?
我尝试添加+
,但得到的只是语法错误。谁能帮我这个?
name = input("What's your name? ")
print("")
print("Hi " + name + "!")
age1 = int(input("How old are you? "))
age2 = age1/2+7
print(age1, "? Cool! Just remember that you can't date anyone younger than " + format(age2,"0.0f") + "!")
解决方案
我尝试添加“+”,但得到的只是语法错误。
您需要替换 ,
为+
:
print(age1 + "? Cool! Just remember that you can't date anyone younger than " + format(age2,"0.0f") + "!")
从 Python 3.6 开始,这可以使用f-strings 1更好地编写:
print(f"{age1}? Cool! Just remember that you can't date anyone younger than {age2:0.0f}!")
1以前的 Python 版本也提供了不同的格式化选项,但现在这些选项大多具有历史意义。
推荐阅读
- android - 为什么即使在 gradle 中添加了自定义映射后,Apollo-android 也没有生成自定义类型?
- git - 修改基本提交时更新所有(本地)受影响的分叉分支
- php - 如何将字符串转换为数字,以便在 select 语句中使用?
- angular - Angular:我应该取消订阅 switchMap
- go - GORM 支持单次插入有很多关系吗?
- matlab - 生成具有矩形网格成对距离的矩阵 -MATLAB
- selenium - 未解决的编译问题:双引号未正确关闭字符串文字
- sql - 在一个函数 PostgreSQL 中创建一个表并将数据插入到该表中
- django-models - 从 models.CharField 中禁用带空白
- json - spark from_json 选项 accept_case_insensitive_properties 如何设置