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?

3
  • Possible duplicate of Correct way to define Python source code encoding Commented Sep 15, 2016 at 18:44
  • What editor were you using to create this file? There were some odd unicode non-standard spaces that caused the SyntaxError. +ode2k for finding other syntax / potnetial runtime errors btw Commented Sep 15, 2016 at 19:17
  • Just using text editor named Geany and executed via command line. Commented Sep 15, 2016 at 20:50

1 Answer 1

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!')
Sign up to request clarification or add additional context in comments.

5 Comments

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..
It appears to be Python3, so raw_input is deprecated
Aah, I forgot about that change... I still primarily use 2.7
Invisible chars in several places actually... unicode narrow no-break space Interesting......
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.