因此,我尝试构建此简单代码来尝试猜测数字:
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中运行代码,则会显示错误:
Value error in line 11, stating Invalid literal for int() with Base 10.
这是什么意思,如何解决?
答案 0 :(得分:4)
您正在尝试将char y
转换为int,而不是将名为y
的变量转换为int
将x=int('y')
替换为x=int(y)
,然后重试
答案 1 :(得分:1)
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'
您正在此处尝试将char
转换为int
,因为该错误会抛出 int()具有基数10的无效文字错误。仅当int
为.isdigit()
时,字符串/ char才能转换为True
。