我现在遇到了很多关于python的问题。我试着计算一个数组中的几个整数。所以它不应该太复杂。我现在已经工作了两天,但是无法让它工作。
def calculate( str ):
global x
if (len(x)==9):
a = []
for i in x_str:
a.append(i)
print(a)
b = 0
for j in range(1,9):
if (type(a[j])==int):
b = b + (a[j] * j)
else:
print('Your x is not a number!')
print(b)
else:
print('Your x is too long or too short.')
isdn = input('Enter your x with 9 numbers')
calculate( x )
# Operation
x = input('How is your x?')
try:
x = str( x )
calculate( x )
except:
print('Not a String!')
我只想输入一个包含9个数字的整数并将其更改为数组。然后用它做一个简单的计算。
我试着这样做,没有那个该死的尝试,除了第一次,但这也不起作用。以某种方式Python不会,当我输入输入时,x是一个字符串。我该怎么做才能让这个计算起作用?它一直告诉我:
SyntaxError: Non-ASCII character '\xe2' in file
我现在非常绝望,因为我无法让它工作......不知何故,Python正在混合字符串和整数,我无法理解为什么。我知道其他语言,但从来没有那么多麻烦让一个简单的计算工作。 有人能指出我的错误吗?或者我能做什么?
答案 0 :(得分:1)
我改变了:
a.append(i)
至a.append(int(i))
global x
for i in x_str:
至for i in x:
isdn
变量更改为x
def calculate( x ):
if (len(x)==9):
a = []
for i in x:
a.append(int(i))
else:
print('Your x is too long or too short.')
x = input('Enter your x with 9 numbers')
calculate( x )
您在x = input
和x = str ( x )
之前也有不良(不可见)字符。如果你想复制/粘贴,我会在下面的代码中清理它们。
x = input('How is your x?')
try:
x = str( x )
calculate( x )
except:
print('Not a String!')