20

I am trying to write a binary search program for a class, and I am pretty sure that my logic is right, but I keep getting a non-UTF-8 error. I have never seen this error and any help/clarification would be great! Thanks a bunch.

Here's the code.

def main():


    str names = [‘Ava Fischer’, ‘Bob White’, ‘Chris Rich’, ‘Danielle Porter’, ‘Gordon Pike’, ‘Hannah Beauregard’, ‘Matt Hoyle’, ‘Ross Harrison’, ‘Sasha Ricci’, ‘Xavier Adams’]

    binarySearch(names, input(str("Please Enter a Name.")))

    print("That name is at position "+position)


def binarySearch(array, searchedValue):

    begin = 0 
    end = len(array) - 1 
    position = -1 
    found = False

    while !=found & begin<=end:
        middle=(begin+end)/2

        if array[middle]== searchedValue:
            found=True 
            position = middle
        elif array[middle] >value:
            end=middle-1
        else:
            first =middle+1
return position
Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
GrapeSoda3
  • 543
  • 1
  • 5
  • 15
  • 6
    There are **many** syntax errors in the posted code. `str names` with a space is not valid Python, the single quotes are not valid ASCII single quotes, Python uses `and`, not `&` for boolean logic, etc. – Martijn Pieters Apr 15 '14 at 19:01
  • What editor did you use to write your code? – Martijn Pieters Apr 15 '14 at 19:02
  • 3
    Is this your _actual_ code, or did you attempt to re-write pseudo-code to describe the problem? – g.d.d.c Apr 15 '14 at 19:04
  • 2
    @g.d.d.c: well, seeing as I can reproduce the error by encoding the OP sample to CP1252 then running it with Python 3, we can assume this is the actual code. – Martijn Pieters Apr 15 '14 at 19:27

5 Answers5

64

Add this line at the top of you code. It may work.

    # coding=utf8
东方魔盗
  • 658
  • 1
  • 5
  • 5
16

Your editor replaced ' (ASCII 39) with U+2018 LEFT SINGLE QUOTATION MARK characters, usually a sign you used Word or a similar wordprocessor instead of a plain text editor; a word processor tries to make your text 'prettier' and auto-replaces things like simple quotes with fancy ones. This was then saved in the Windows 1252 codepage encoding, where the fancy quotes were saved as hex 91 characters.

Python is having none of it. It wants source code saved in UTF-8 and using ' or " for quotation marks. Use notepad, or better still, IDLE to edit your Python code instead.

You have numerous other errors in your code; you cannot use spaces in your variable names, for example, and Python uses and, not & as the boolean AND operator. != is an operator requiring 2 operands (it means 'not equal', the opposite of ==), the boolean NOT operator is called not.

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
  • Alright I fixed all the syntax errors, but now it is giving me (TypeError: list indices must be integers, not float) I cannot enter a string into the search field. – GrapeSoda3 Apr 15 '14 at 19:38
  • 1
    @user3352542: use `//` instead of `/` to get integer (floor) division instead. – Martijn Pieters Apr 15 '14 at 20:42
4

If you're using Notepad++, click Encoding at the top and choose Encode in UTF-8.

Michael Kiros
  • 373
  • 3
  • 11
1

The character you are beginning your constant strings with is not the right string delimiter. You are using

‘Ava Fischer’   # ‘ and ’ as string delimiters

when it should have been either

'Ava Fischer'   # Ascii 39 as string delimiter

or maybe

"Ava Fischer"   # Ascii 34 as string delimiter
-1

Add this line to the top of your code, it might help

# -*- coding:utf-8 -*-

Yellow
  • 7
  • 2