简体   繁体   English

Traceback(最近一次通话最后一次)Python 错误

[英]Traceback (most recent call last) Python Error

Error错误

Traceback (most recent call last):
  File "C:\Users\Alex\Python\Lesson01.py", line 5, in <module>
    print("In 10 years you will be ", a+b, "years old")
TypeError: can only concatenate str (not "int") to str

At the moment I am Learning Python 3 and got this error.目前我正在学习 Python 3 并收到此错误。 What does it mean?这是什么意思? I also tried + instead of the comma this is also not working:/我也尝试过+而不是逗号,这也不起作用:/

Here is the code:这是代码:

user_input = input("How old are you?\n-> ")

a = user_input
b = 10
print("In 10 years you will be ", a+b, "years old")

The answer above is correct but I would argue some better naming schemes would improve the readability and usefulness of the code...上面的答案是正确的,但我认为一些更好的命名方案会提高代码的可读性和实用性......

age = int(input("How old are you?\n-> "))

numYears = 10

print("In " + str(numYears) + " years you will be " + str(age+numYears) + " years old")

The point of this way is to make the print statement more versatile, also avoid having duplicate variables where it isn't necessary, and to have better variable names.这种方式的重点是使 print 语句更加通用,同时避免在不必要的地方使用重复的变量,并具有更好的变量名称。

You have to print this:你必须打印这个:

user_input = int(input("How old are you?\n-> "))

a = user_input
b = 10
print("In 10 years you will be " + str(a+b) + " years old")

this should work fine这应该可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM