Python初學者錯誤:TypeError: unsupported operand type(s) for +: 'int' and 'str'
在大神們看來print可能是一個再簡單不過的函式,但是對於新手來說,在自學過程中,會碰到各種各樣的問題,如果沒有很好的老師或者學習思路,很有可能學習成本會相當高,所以希望大神們不要鄙視初學者的“傻X”問題,畢竟大家都是過來人
如下程式碼:
>>> a = 1
>>> print(a)
1
>>> b = 2
>>> print(a + b)
3
>>> type(a)
<type 'int'>
此時我們很想打印出如下效果:
>>> print(str (a) + '*' + str(b) + '=' + str(a * b))
1*2=2
可是我們不知道為什麼要加’str’,於是就出現如下錯誤:
>>> print(a + '*' + b + '=' + a * b)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
其實錯誤提示已經很明確了,“型別錯誤:不支援操作型別為整數和字串”,這裡需要解釋的最關鍵的東西是“+”,“+”在python中有兩個作用,一個是數學運算子,是用來在整型、浮點型等數學之間進行加法操作的。另一個是用來進行字串連線的。所以當你的“+”出現在即有數學運算和字元連線的情況下,計算機根本不知道哪兩個是要進行字串連線,哪兩個之間要進行數學運算。
舉一反三:
>>> print('a' + 2)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print(2 + "a")
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type (s) for +: 'int' and 'str'
相關推薦
Python初學者錯誤:TypeError: unsupported operand type(s) for +: 'int' and 'str'
在大神們看來print可能是一個再簡單不過的函式,但是對於新手來說,在自學過程中,會碰到各種各樣的問題,如果沒有很好的老師或者學習思路,很有可能學習成本會相當高,所以希望大神們不要鄙視初學者的“傻X”問題,畢竟大家都是過來人 如下程式碼: &
TypeError: unsupported operand type(s) for +: 'int' and ‘str’問題原因記錄
錯誤型別:TypeError: unsupported operand type(s) for +: 'int' and ‘str’錯誤程式碼:age=input('please put in your age: ‘) ag
【Python】關於鍵盤鍵入值、str的與或非問題?【報錯:TypeError: unsupported operand type(s) for |: 'str' and 'str'】
error 運算符 字符 符號 str == 條件 col one 參考 【報錯:TypeError: unsupported operand type(s) for |: ‘str‘ and ‘str‘】 在進行鍵入值比較的時候,想要用“或&rd
[機器學習實戰-ch09]TypeError: unsupported operand type(s) for /: 'map' and 'int'
在執行這一段命令的最後一行時出現了問題 >>>reload(regTrees) <module 'regTrees' from 'regTrees.pyc'> >>> from numpy import *
解決Python中sum函數出現的TypeError: unsupported operand type(s) for +: 'int' and 'list'錯誤問題
當在Python中執行sum函式時,會出現“TypeError: unsupported operand type(s) for +: 'int' and 'list'”這樣的問題 程式碼如: print(sum([[1, 2, 3], [4, 5, 5]])) 會出現如下的錯誤:
python2 連線mysql獲取值出現的錯誤TypeError: unsupported operand type(s) for /: 'float' and 'NoneType'
我這是一個獲取實時匯率的方法,剛開始寫法如下就是一直會報一個數據型別錯誤 獲取匯率 def get_rate(self, time): # Where USD is the base currency you want to use url = 'https://v
解決pip安裝時出現報錯TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
我在Linux下使用pip安裝時出現報錯: Exception: Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in m
TypeError: unsupported operand type(s) for /: 'map' and 'int'
出現下面這樣的錯誤: ret, rcount, out=ret, casting='unsafe', subok=False) TypeError: unsupported operand type(s) for /: 'map' and 'int' 出錯位置: data
dataframe 對列資料進行資料操作時發生TypeError: unsupported operand type(s) for /: 'str' and 'int'
相對dataframe中“最高價”列資料進行除以2操作, data = pd.DataFrame(PriceList,columns=['品名','規格','max1','平均價','最低價','產地','日期'],index=range(1,len(PriceLi
unsupported operand type(s) for *: 'float' and 'decimal.Decimal'
unsupported operand type(s) for *: 'float' and 'decimal.Decimal' co=round(co/2,3) co_aqi=AqiCommon.getIAQI(co,'co_hour')報錯 co_aqi=AqiCom
Python中錯誤之 TypeError: object() takes no parameters、TypeError: this constructor takes no arguments
obj blog img typeerror str mage 劃線 es2017 http TypeError: object() takes no parameters TypeError: this constructor takes no arguments
Python常見錯誤:IndexError: list index out of range
spa bsp lis 遍歷字典 pan nbsp exe 字典 dex 用python寫腳本查詢字典時,在遍歷字典時循環到某一項時老是報錯 出現這種錯誤有兩種情況: 第1種可能情況 list[index]index超出範圍 第2種可能情況 list是空值就會出現 In
第一次使用jupyter建立python時錯誤:Creating Notebook Failed An error occurred while creating a new notebook.
當我們用命令列在瀏覽器中啟動jupyter notebook後需要點選右上角的New建立新的python檔案,此時會報錯。在Linux下可以用命令列解決這個問題。首先需要找到anaconda2中jupyter檔案,在anaconda2中的share檔案中,對jupyter執行如下命令,來修改jupy
python報錯:TypeError: slice indices must be integers or None or have an __index__ method
宣告:本文為博主原創文章,不可轉載 https://blog.csdn.net/jjddss/article/details/73469104 在使用Python進行矩陣操作時,當內部含有除法時,會產生錯誤: TypeError: slice indices must be integers
eclipse錯誤:Access restriction: The type 'BASE64Decoder' is not API
Access restriction: The type ‘BASE64Decoder’ is not API (restriction on required library ‘D:\java\jdk1.7.0_45\jre\lib\rt.jar’) 解決其實很簡單,把JRE System Library
python爬蟲問題:TypeError: cannot use a string pattern on a bytes-like objec
Python3.x在學到爬蟲是需要注意不同於Python2.x需要將html進行解碼: import urllib import re def download(url,user_agent='XD',num_retries=2): print
Python異常:TypeError: a bytes-like object is required, not 'str'
Python異常:TypeError: a bytes-like object is required, not ‘str’ import json str = [{"userName" : "Use
註解配置實體類出現錯誤:Could not determine type for: java.util.Set, at table
org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: bs_merchant, for columns: [org.hibernate.mapping.Co
【我的Android進階之旅】修復錯誤:Use of generics not allowed for java type at 'com.fasterxml.jackson.databind’
一、問題描述 剛把app專案的compileSdkVer 改為 28,buildToolsVer改為"28.0.3"之後,編譯失敗了,失敗錯誤如下所示: Warning: Exception while processing task java.io.IOException
NodeJs錯誤:TypeError: Cannot read property 'path' of undefined
情景重現: 錯誤程式碼: function upload(response, request){ console.log("Request handler 'upload' was called."); var form = new formidable.Incom