【发布时间】: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 行,但没有任何效果。我已经回到课程并在线查看了其他代码示例,但我似乎无法弄清楚我做错了什么。我收到的第二种错误消息是无效语法
【问题讨论】:
-
int(input("Which year were you born?")应该给出 SyntaxError,因为它缺少右括号。不是类型错误。 -
欢迎来到堆栈溢出!请拍tour。此代码不会引发该错误,它会引发 SyntaxError。一旦你修正了错别字,如果你仍然有困难,请阅读How to Ask并制作一个minimal reproducible example。另见How to ask and answer homework questions。
标签: python