Python 试图编写一份我不知道的调查';I don’我不知道如何使减法生效
我正在尝试编写一个调查类型的代码。我要做的一件事就是计算你的年龄。 有人能告诉我这个代码有什么问题吗Python 试图编写一份我不知道的调查';I don’我不知道如何使减法生效,python,python-3.x,input,subtraction,survey,Python,Python 3.x,Input,Subtraction,Survey,我正在尝试编写一个调查类型的代码。我要做的一件事就是计算你的年龄。 有人能告诉我这个代码有什么问题吗 birth = int(input('what year were you born in?')) year = int(input('what year is it?')) print(int(year-birth) + ' that is your age') 您正在尝试将int添加到str中,因此在连接之前可以将其转换为字符串 print(str(year-birth) + ' tha
birth = int(input('what year were you born in?'))
year = int(input('what year is it?'))
print(int(year-birth) + ' that is your age')
您正在尝试将
int
添加到str
中,因此在连接之前可以将其转换为字符串
print(str(year-birth) + ' that is your age')
或者使用str.format
print('{} that is your age'.format(year-birth))
或者使用和f-string
print(f'{year-birth} that is your age')
您好,您可以使用下面的代码
birth = int(input('what year were you born in?'))
year = int(input('what year is it?'))
print((year-birth) , 'that is your age')
正如你所说的,int是你的出生和年份,程序会理解你是把2 int加在一起而不是字符串。另一件事是,您不能同时添加int和字符串,因此需要使用逗号
希望这有帮助!:D什么使您认为有问题?您收到一条错误消息:
TypeError:不支持+:'int'和'str'的操作数类型。
。您应该将此错误消息与您的问题一起发布。除此之外,它告诉您问题是什么-您不能添加字符串和整数值。或者,使用f-stringprint(f“{年出生}即您的年龄”)
f-strings
也是一种很好的打印方式。从python3.6开始使用。