1

I've got quite a problem with python right now. I try around to just calculate a few integers in an array. So it should not be too complicated. I worked on it over a 2 days now, but can't get it to work.

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!')

I just want to input an integer with 9 numbers and change that to an array. And do a simple calculation then with it.

I tried to do it without that damn try and except first, but that does not work either. Somehow Python won't except that x is a string when I enter it with input. What can I do, to get this calculation to work? It keeps telling me:

SyntaxError: Non-ASCII character '\xe2' in file

I am really desperate right now, since I cannot get it to work.... Somehow Python is mixing strings and integers up and I cannot understand why. I know other languages, but never had that much trouble to get a simple calculation to work. Does anyone can point me out my mistake? Or what I can do?

ode2k
  • 2,653
  • 13
  • 20
Severus15
  • 127
  • 9
  • Possible duplicate of [Correct way to define Python source code encoding](http://stackoverflow.com/questions/728891/correct-way-to-define-python-source-code-encoding) – felipsmartins Sep 15 '16 at 18:44
  • What editor were you using to create this file? There were some odd [unicode](http://www.fileformat.info/info/unicode/char/202f/index.htm) non-standard spaces that caused the `SyntaxError`. +ode2k for finding other syntax / potnetial runtime errors btw – Aaron Sep 15 '16 at 19:17
  • Just using text editor named Geany and executed via command line. – Severus15 Sep 15 '16 at 20:50

1 Answers1

1

I changed:

  • a.append(i) to a.append(int(i))
  • removed global x
  • for i in x_str: to for i in x:
  • changed your isdn variable to 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 )

You also had bad (invisible) characters before the x = input and the x = str ( x ). I cleaned them up in the code below if you want to copy/paste.

x = input('How is your x?')
try:
    x = str( x )
    calculate( x )
except:
    print('Not a String!')
ode2k
  • 2,653
  • 13
  • 20
  • python `input` uses `eval` so he's not getting a string in the first place likely.. Use `raw_input` instead. Also something fishy about `isdn` variable in else statement you may want to look at.. – Aaron Sep 15 '16 at 19:00
  • It appears to be Python3, so `raw_input` is deprecated – ode2k Sep 15 '16 at 19:01
  • Aah, I forgot about that change... I still primarily use 2.7 – Aaron Sep 15 '16 at 19:04
  • Invisible chars in several places actually... unicode [narrow no-break space](http://www.fileformat.info/info/unicode/char/202f/index.htm) Interesting...... – Aaron Sep 15 '16 at 19:14
  • @Aaron. Thank you for cleaning up that code. Never noticed the invisible characters. I only use a Text Editor named Geany and execute the file via command line. I copied and paste it with Kate now. Still not running correctly though, just get a different error, so it always prints out: Not a String. Regardless of whats written. Tried with 123456789 and 987654321. – Severus15 Sep 15 '16 at 20:45