【问题标题】:MOOCs Python Programming exercise: Name and age [closed]MOOCs Python 编程练习:姓名和年龄 [关闭]
【发布时间】:2023-01-03 00:05:19
【问题描述】:

我是编码方面的新手,Python 将是我的第一语言。我正试图弄清楚这一点,但我一直在试图了解我所缺少的是什么。

锻炼

请编写一个程序,询问用户的姓名和出生年份。然后程序打印出一条消息如下:

示例输出

What is your name? Frances Fictitious
Which year were you born? 1990
Hi Frances Fictitious, you will be 31 years old at the end of the year 2021

我的代码

name = input("What is your name?")
born = int(input("Which year were you born?")
year = (2021-born)
print(f"Hi " + name + "you will be {2021-year} years old at the end of the year 2021" )

错误信息

TypeError on line 3: unsupported operand type(s) for -: 'int' and 'str'

我已经完全删除了“年”,将输入设为 int 并多次更改第 3 行,但没有任何效果。我已经回到课程并在线查看了其他代码示例,但我似乎无法弄清楚我做错了什么。我收到的第二种错误消息是无效语法

【问题讨论】:

标签: python


【解决方案1】:

您缺少右括号,并且打印时也有错误。试试这个,应该有用:

name = input("What is your name?")
born = int(input("Which year were you born?"))
print(f"Hi {name}, you will be {2021-born} years old at the end of the year 2021")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多