2014-11-05 55 views
-1

我试图创建一个程序,要求用户输入年份Haley的彗星最后一次出现,然后再添加75和76年,然后输出结果。无法将int int更改为str隐藏

lastsight = int(input("When was the last sighting? ")) 
firstsight = lastsight + 75 
secondsight = lastsight + 76 
print("The next sighting will be in", firstsight, "or", secondsight + ".") 

但是,每当我运行它,我得到这个错误:

Traceback (most recent call last): 
File "program.py", line 5, in <module> 
print("The next sighting will be in", firstsight, "or", secondsight + ".") 
TypeError: unsupported operand type(s) for +: 'int' and 'str' 

我如何能解决这个问题的任何帮助,将不胜感激。

回答

2

使用以下命令:

print("The next sighting will be in", firstsight, "or", str(secondsight) + ".") 

正如你正确地观察到,蟒蛇不会隐执行这些转换。

2

试试这个:

print("The next sighting will be in {} or {}.".format(firstsight, secondsight)) 
相关问题