创建一个python程序,告诉我名字和出生年份的人的年龄

2024-06-16 11:18:10 发布

您现在位置:Python中文网/ 问答频道 /正文

雷蒙德1962

艾米1982

杰克1978

凯文1970

罗莎1981

查尔斯1970

特里1968

吉娜1978

我必须创建一个程序,要求用户的名字,他们想知道的人的年龄。你知道吗

# Storing name information

names = ['Raymond', 'Amy', 'Jake', 'Kevin', 'Rosa', 'Charles', 'Terry', 'Gina']

# Assigning year of birth 

YOB = ['1962', '1982', '1978', '1970', '1981', '1970', '1968', ',1978']

# Assigning each name in the form of a string to an integer value

Raymond = 1962
Amy = 1982
Jake = 1978
Kevin = 1970
Rosa = 1981
Charles = 1970
Terry = 1968
Gina = 1978
a = 2019

names = input('Who is the person you want to know the age of')
print('Their age is:', a - names)

这就是我目前所拥有的。你知道吗

Line 22: TypeError: unsupported operand type(s) for Sub: 'int' and 'str'.

这是我运行它时的错误消息


Tags: ofthetonamenamesisterrycharles
1条回答
网友
1楼 · 发布于 2024-06-16 11:18:10

不能从int中减去字符串。此外,YOB列表包含字符串,而不是数字。你知道吗

为什么不用字典呢?你知道吗

namesYOB = {
    'Raymond': 1962,
    'Amy': 1982,
    'Jake': 1978,
    'Kevin': 1970,
    'Rosa': 1981,
    'Charles': 1970,
    'Terry': 1968,
    'Gina': 1978
}
a = 2019

name = input('Who is the person you want to know the age of')

if name in namesYOB:
    print('Their age is:', a - namesYOB[name])
else:
    print('Specified person not found')

相关问题 更多 >