首页 > 解决方案 > 为什么不能删除变量后面的空格?

问题描述

我似乎无法摆脱最后一行之间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") + "!")

标签: python

解决方案


我尝试添加“+”,但得到的只是语法错误。

您需要替换 ,+

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 版本也提供了不同的格式化选项,但现在这些选项大多具有历史意义。


推荐阅读