簡體   English   中英

Traceback(最近一次通話最后一次)Python 錯誤

[英]Traceback (most recent call last) Python 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

目前我正在學習 Python 3 並收到此錯誤。 這是什么意思? 我也嘗試過+而不是逗號,這也不起作用:/

這是代碼:

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

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

上面的答案是正確的,但我認為一些更好的命名方案會提高代碼的可讀性和實用性......

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

numYears = 10

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

這種方式的重點是使 print 語句更加通用,同時避免在不必要的地方使用重復的變量,並具有更好的變量名稱。

你必須打印這個:

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")

這應該可以正常工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM