无法将int转换为字符串

时间:2014-02-23 20:57:30

标签: python string 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 个答案:

答案 0 :(得分:2)

cif == str(cif)

应该是

cif = str(cif)

在Python中,==是比较,=是赋值。

答案 1 :(得分:1)

您可以在str()功能中使用print

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

另请注意,==用于检查相等性。使用=进行分配。使用

cif = str(cif)

而不是

cif == str(cif)