2014-02-23 140 views
0

我試圖運行此代碼,但它給了我以下錯誤:不能將int轉換成字符串

Traceback (most recent call last): 
File "C:/Users/Admin/Desktop/gess.py", line 29, in <module> 
print('arasworia, chemi chafiqrebuli cifri iyo ' + cif) 
TypeError: Can't convert 'int' object to str implicitly 

下面是代碼:

import random 
guessTaken = 0 
print(' ra gqvia? ') 
sax = input() 
cif = random.randint(1, 10) 
print('Gamarjoba ' + sax + ' me vfiqrob cifrs, romelic aris 1 dan 10-mde! da gaqs 3 cda ') 
while guessTaken < 3: 
print(' chaifiqre cifri') 
vard = input() 
vard = int(vard) 

guessTaken = guessTaken + 1 

if vard < cif: 
    print(' Ufro dabal cifrs vfiqrob ') 

if vard > cif: 
    print(' ufro magal cifrs vqirob ') 

if vard == cif: 
    break 

if vard == cif: 
guessTaken = str(guessTaken) 
print('gilocav ' + sax + 'shen gamoicane cifr ' + guessTaken + ' cdashi!') 

if vard != cif: 
cif == str(cif) 
print('arasworia, chemi chafiqrebuli cifri iyo ' + cif) 

回答

2

cif == str(cif) 

應該是

cif = str(cif) 

在Python中,==是比較值,=是賦值。

1

您可以使用str()print函數內部:

print('arasworia, chemi chafiqrebuli cifri iyo ' + str(cif)) 

另外,請注意==用於檢查平等。使用=進行分配。使用

cif = str(cif) 

,而不是

cif == str(cif) 
+0

爲什麼downvote?歡迎反饋。 – Christian

+0

這應該是+1。比較運算符錯誤是解決方案。 – Will