Python 基数为10的int()的文本无效'';

Python 基数为10的int()的文本无效'';,python,python-2.7,Python,Python 2.7,因此,我尝试构建这个简单的代码来猜测一个数字: y = raw_input() print type(y) x = int('y') print type(x) if x > 0: print 'positive' if x > 10: print 'Greater than 10' else: print 'less than 10' elif x == 0: print 'equals 0' print 'Cond

因此,我尝试构建这个简单的代码来猜测一个数字:

y = raw_input()
print type(y)
x = int('y')
print type(x)
if x > 0:
    print 'positive'
    if x > 10:
        print 'Greater than 10'
    else:
        print 'less than 10'

elif x == 0:
   print 'equals 0'

print 'Conditionals are over'
print 'Bye Bye'
但是,在windows powershell中运行代码时,会显示一个错误:


这意味着什么?如何修复它?

您试图将char
y
转换为int,而不是将名为
y
的变量转换为int

x=int('y')
替换为
x=int(y)
,然后重试
y=raw_input()
print type(y)
x=int(y) # < -- Do not pass y as string. 'y' ==> y
print type(x)
if x>0:
    print 'positive'
    if x>10:
        print 'Greater than 10'
    else:
        print 'less than 10'

elif x==0:
   print 'equals 0'

print 'Conditionals are over'
print 'Bye Bye'
打印类型(y) x=int(y)#<--不要将y作为字符串传递。”y'==>y 打印类型(x) 如果x>0: 打印“正片” 如果x>10: 打印“大于10” 其他: 打印“小于10” elif x==0: 打印“等于0” 打印“条件结束” 打印“再见”
您正试图在此处将
char
转换为
int
,因此会抛出无效的文本for int(),以10为基数。只有当
.isdigit()
True
时,字符串/字符才能转换为
int

我猜您还没有发布所有代码,基本上您正在尝试将字符串转换为int(int(“y”),使用int(y),
x=int('y')
将值为
'y'
的字符串传递到
int()
函数中。请注意与
type()的对比
call,在这里输入
y
变量。
y=raw_input()
print type(y)
x=int(y) # < -- Do not pass y as string. 'y' ==> y
print type(x)
if x>0:
    print 'positive'
    if x>10:
        print 'Greater than 10'
    else:
        print 'less than 10'

elif x==0:
   print 'equals 0'

print 'Conditionals are over'
print 'Bye Bye'