【问题标题】:OCR Unexpected lndentOCR 意外缩进
【发布时间】:2019-02-13 11:48:48
【问题描述】:

我需要有关用于 OCR 的 NEA 代码的帮助。我遇到了“意外缩进”错误代码,需要一些关于为什么我会收到此错误的建议

def create_account(username,password):
    with open("Users.csv", "a", newline="") as csvfile:
        appender=csv.writer(csvfile)
        appender.writerow([username,password])

import csv
###MENU######MENU######MENU######MENU######MENU######MENU######MENU###
def display_song(artist, song):
    print(artist) #Print Name Of Song Artist
    words= song.split() #Splits the words of the song
    for current_word in words: #
        no_of_letters = len(current_word) # Counts the amount of letters in the song name
        print(current_word[0]+"_"*(no_of_letters-1)+" ",end="")

def menu(options):
    no_of_options = len(options)
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nn===================================================")
    print("¦ Please choose from one of the following options ¦")
    print("===================================================")
    for each_option in range (no_of_options):
        print("¦"+str(each_option+1)+") "+options[each_option])
    print("===================================================")
    user_input=input("¦  Please Enter an option  ¦> ")
    try:
        option_converted_to_integer = int(user_input)
        if option_converted_to_integer > 0 and option_converted_to_integer <= no_of_options:
            return(option_converted_to_integer)
        else:
            raise
    except:
        print(str(user_input) + " is not a valid option, please try again.")
        return menu(options)

    with open ("Users.csv") as csvfile:
            reader=csv.reader(csvfile,delimiter=",")
            for row in reader:import csv
import random
###MENU######MENU######MENU######MENU######MENU######MENU######MENU###
def display_song(artist, song):
    print(artist) #Print Name Of Song Artist
    words= song.split() #Splits the words of the song
    for current_word in words: #
        no_of_letters = len(current_word) # Counts the amount of letters in the song name
        print(current_word[0]+"_"*(no_of_letters-1)+" ",end="")

def menu(options):
    no_of_options = len(options)
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n===================================================")
    print("¦ Please choose from one of the following options ¦")
    print("===================================================")
    for each_option in range (no_of_options):
        print("¦"+str(each_option+1)+") "+options[each_option])
    print("===================================================")
    user_input=input("¦  Please Enter an option  ¦> ")
    try:
        option_converted_to_integer = int(user_input)
        if option_converted_to_integer > 0 and option_converted_to_integer <= no_of_options:
            return(option_converted_to_integer)
        else:
                song_list.append(row[0])
                full_song_list.append(row[0])
                artist_list.append(row[0])

def create_account(username,password):
    with open("Users.csv", "a", newline="") as csvfile:
        appender=csv.writer(csvfile)
        appender.writerow([username,password])
###SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING###
print('\n'*69)
menu_options=["Log In","Create an Account","Quit the program"]
selected_options = menu(menu_options)
while selected_options!=len(menu_options):
        def game():
                    song_list=[]
                    global songlist
                    full_song_list=[]
                    global full_song_list
                    artist_list=[]
                    global artist_list
                    point=0
                    global points
                    successful_guesses=0
                    with open ("song names.csv") as csvfile:
                        reader=csv.reader(csvfile,delimiter=",")
                        for row in reader:
                            song_list.append(row[0])
                            full_song_list.append(row[0])
                            artist_list.append(row[0])
                            lives=1
                            while True:
                                song_number=random.randint(0,20)
                                song=song_list[song_number]
                                song_to_guess=full_song_list[song_number]
                                #print("DEBUG: ",song_to_guess,"\n")
                                print(song)
                                print(artist_list[song_number])
                                guess=input("Please guess the artist:\n")
                                song=song.lower()
                                if guess==song_to_guess:
                                    if lives==2:
                                        print("\nCorrect!\n")
                                        while True:
                                            if user_option==1:
                                                log_in()
                                                while True:
                                                    user_option=menu(["Start Game","Exit"])
                                                    print(user_option)
                                                    if user_option==1:
                                                        shown_username=import csv
import random
###MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU###
def display_song(artist, song):
    print(artist) #Print Name Of Song Artist
    words= song.split() #Splits the words of the song
    for current_word in words: #
        no_of_letters = len(current_word) # Counts the amount of letters in the song name
        print(current_word[0]+"_"*(no_of_letters-1)+" ",end="")

def menu(options):
    no_of_options = len(options)
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n===================================================")
    print("¦ Please choose from one of the following options ¦")
    print("===================================================")
    for each_option in range (no_of_options):
        print("¦"+str(each_option+1)+") "+options[each_option])
    print("===================================================")
    user_input=input("¦  Please Enter an option  ¦> ")
    try:
        option_converted_to_integer = int(user_input)
        if option_converted_to_integer > 0 and option_converted_to_integer <= no_of_options:
            return(option_converted_to_integer)
        else:
            raise
    except:
        print(str(user_input) + " is not a valid option, please try again.")
        return menu(options)

    with open ("Users.csv") as csvfile:
            reader=csv.reader(csvfile,delimiter=",")
            for row in reader:import csv
import random
###MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU######MENU###
def display_song(artist, song):
    print(artist) #Print Name Of Song Artist
    words= song.split() #Splits the words of the song
    for current_word in words: #
        no_of_letters = len(current_word) # Counts the amount of letters in the song name
        print(current_word[0]+"_"*(no_of_letters-1)+" ",end="")

def menu(options):
    no_of_options = len(options)
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n===================================================")
    print("¦ Please choose from one of the following options ¦")
    print("===================================================")
    for each_option in range (no_of_options):
        print("¦"+str(each_option+1)+") "+options[each_option])
    print("===================================================")
    user_input=input("¦  Please Enter an option  ¦> ")
    try:
        option_converted_to_integer = int(user_input)
        if option_converted_to_integer > 0 and option_converted_to_integer <= no_of_options:
            return(option_converted_to_integer)
        else:
                song_list.append(row[0])
                full_song_list.append(row[0])
                artist_list.append(row[0])

def create_account (username,password):
   with open("Users.csv", "a", newline="") as csvfile:
        appender=csv.writer(csvfile)
        appender.writerow([username,password])
###SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING######SIGN UP, LOG IN, QUITTING###
print('\n'*69)
menu_options=["Log In","Create an Account","Quit the program"]
selected_options = menu(menu_options)
while selected_options!=len(menu_options):
        def game():
                    song_list=[]
                    global songlist
                    full_song_list=[]
                    global full_song_list
                    artist_list=[]
                    global artist_list
                    point=0
                    global points
                    successful_guesses=0
                    with open ("song names.csv") as csvfile:
                        reader=csv.reader(csvfile,delimiter=",")
                        for row in reader:
                            song_list.append(row[0])
                            full_song_list.append(row[0])
                            artist_list.append(row[0])
                            lives=1
                            while True:
                                song_number=random.randint(0,20)
                                song=song_list[song_number]
                                song_to_guess=full_song_list[song_number]
                                #print("DEBUG: ",song_to_guess,"\n")
                                print(song)
                                print(artist_list[song_number])
                                guess=input("Please guess the artist:\n")
                                song=song.lower()
                                if guess==song_to_guess:"guest"
                                                        pass
                                                    elif user_option==2:
                                                        game()
                                                        if user_option==2:
                                                            entered_name=input("What is your name?\n")
                                                            if selected_options==2:
                                                                pass

###LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN######LOG IN###
        if selected_options==1:
            print('\n'*68)
            input('Enter your username >')
            input('Enter your password >')
            pass
            menu_options=["Start Game","Quit the program"]
            selected_options = menu(menu_options)
            if selected_options==1:
                def game():
                    song_list=[]
                    global songlist
                    full_song_list=[]
                    global full_song_list
                    artist_list=[]
                    global artist_list
                    point=0
                    global points
                    successful_guesses=0
                    with open ("song names.csv") as csvfile:
                        reader=csv.reader(csvfile,delimiter=",")
                        for row in reader:
                            song_list.append(row[0])
                            full_song_list.append(row[0])
                            artist_list.append(row[0])
                            lives=1
                            while True:
                                song_number=random.randint(0,20)
                                song=song_list[song_number]
                                song_to_guess=full_song_list[song_number]
                                #print("DEBUG: ",song_to_guess,"\n")
                                print(song)
                                print(artist_list[song_number])
                                guess=input("Please guess the artist:\n")
                                song=song.lower()
                                if guess==song_to_guess:
                                    if lives==2:
                                        print("\nCorrect!\n")
                                        while True:
                                            if user_option==1:
                                                log_in()
                                                while True:
                                                    user_option=menu(["Start Game","Exit"])
                                                    print(user_option)
                                                    if user_option==1:
                                                        shown_username="guest"
                                                        pass
                                                    elif user_option==2:
                                                        game()
                                                        if user_option==2:
                                                            entered_name=input("What is your name?\n")
                                                            if selected_options==2:
                                                                pass

###CREATING AN ACCOUNT######CREATING AN ACCOUNT######CREATING AN ACCOUNT######CREATING AN ACCOUNT######CREATING AN ACCOUNT######CREATING AN ACCOUNT######CREATING AN ACCOUNT######CREATING AN ACCOUNT######CREATING AN ACCOUNT###
        if selected_options==2:
            print('\n'*68)
            create_account(input("Enter your desired username >"), input("Enter your passsword >"))
            pass
        menu_options=["Start Game","Quit the program"]
        selected_options = menu(menu_options)
        if selected_options==1:
            def game():
                song_list=[]
                global songlist
                full_song_list=[]
                global full_song_list
                artist_list=[]
                global artist_list
                point=0
                global points
                successful_guesses=0
                with open ("song names.csv") as csvfile:
                    reader=csv.reader(csvfile,delimiter=",")
                    for row in reader:
                        song_list.append(row[0])
                        full_song_list.append(row[0])
                        artist_list.append(row[0])
                        lives=1
                        while True:
                            song_number=random.randint(0,20)
                            song=song_list[song_number]
                            song_to_guess=full_song_list[song_number]
                            #print("DEBUG: ",song_to_guess,"\n")
                            print(song)
                            print(artist_list[song_number])
                            guess=input("Please guess the artist:\n")
                            song=song.lower()
                            if guess==song_to_guess:
                                if lives==2:
                                    print("\nCorrect!\n")
                                    while True:
                                        if user_option==1:
                                            log_in()
                                            while True:
                                                user_option=menu(["Start Game","Exit"])
                                                print(user_option)
                                                if user_option==1:
                                                    shown_username="guest"
                                                    pass
                                                elif user_option==2:
                                                    game()
                                                    if user_option==2:
                                                        entered_name=input("What is your name?\n")
                                                        if selected_options==2:
                                                            pass
###QUITTING THE GAME######QUITTING THE GAME######QUITTING THE GAME######QUITTING THE GAME######QUITTING THE GAME######QUITTING THE GAME######QUITTING THE GAME######QUITTING THE GAME######QUITTING THE GAME######QUITTING THE GAME###

if selected_options==3:
                    pass
##STARTING THE GAME###

#Song List
def game():
    global song_list
    song_list=[]
    global full_song_list
    full_song_list=[]
    global artist_list
    artist_list=[]

    points=0

    global points
    successfuI_guesses=0
    global successful_guesses

    with open ("songs.csv") as csvfile:
            reader=csv.reader(csvfile,delimiter=",")
            for row in reader:
                song_list.append(row[0])
                full_song_list.append(row[0])
                artist_list.append(row[0])

            lives=1

            while True:
                song_number=random.randint(0,20)
                song=song_list[song_number]
                song_to_guess=full_song_list[song_number]
                #print("DEBUG: ",song_to_guess,"\n")
                print(song)
                print(artist_list[song_number])
                guess=input("Please guess the artist:\n")
                song=song.lower()
                guess=guess.lower()

                if guess==song_to_guess:
                    if lives==2:
                        print("\nCorrect!\n")
                        points+=3
                        successful_guesses+=1
                    elif lives==1:
                        print("\nCorrect!\n")
                        points=+1
                        successful_guesses=+1



def SignUp():
    userfile = open("Users.csv", "a")
    sUser = input("\nChoose a username: ")
    sPass = input("Choose a password: ")
    sQues = int(input("""
--------------------------------------------------------------------------------
Security Questions:
1. What is/was your first pet's name?
2. Which town were you born in?
3. What is your mother's maiden name?
4. At what age did you buy your first car?
5. How many seasons of Spongebob have you watched?
6. What secondary school did you go to?
--------------------------------------------------------------------------------
Choose a Security Question (1-6): """))
    s1 = ("\nWhat is/was your first pet's name?")
    s2 = ("\nWhich town were you born in?")
    s3 = ("\nWhat is your mother's maiden name?")
    s4 = ("\nAt what age did you buy your first car?")
    s5 = ("\nHow many seasons of Spongebob have you watched?")
    s6 = ("\nWhat secondary school did you go to?")
    answered = False
    while answered == False:
        if sQues == 1:
            print(s1)
            sAns = input("Answer: ")
        if sQues == 4:
            print(s4)
            sAns = input("Answer: ")
            answered = True
        if sQues == 5:
            print(s5)
            sAns = input("Answer: ")
            answered = True
        if sQues == 6:
            print(s6)
            sAns = input("Answer: ")
            answered = True
    WriteLine = sUser + "," + sPass + "," + sAns + "\n"
    userfile.write(WriteLine)
    userfile.close()
    print("\nAccount Created!")
    Menu()

【问题讨论】:

  • 能否请您发布整个代码?
  • 您发布的代码的缩进没有任何问题。解释器指示缩进错误在哪一行。更仔细地查看,如果您仍然看不到问题,请发布包含错误点的代码。
  • 运行代码你会看到错误

标签: python ocr


【解决方案1】:

对于第 52 行中的try(函数menu),您没有编写exceptelse。这就是您收到IndentationError 的原因。

以下代码将使IndentationError消失,语法明智。

    def menu(options):
        no_of_options = len(options)
        print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n===================================================")
        print("¦ Please choose from one of the following options ¦")
        print("===================================================")
        for each_option in range (no_of_options):
            print("¦"+str(each_option+1)+") "+options[each_option])
        print("===================================================")
        user_input=input("¦  Please Enter an option  ¦> ")
        try:
            option_converted_to_integer = int(user_input)
            if option_converted_to_integer > 0 and option_converted_to_integer <= no_of_options:
                return(option_converted_to_integer)
            else:
                song_list.append(row[0])
                full_song_list.append(row[0])
                artist_list.append(row[0])
        except:
            pass
        else:
            pass

您已经定义了两次display_songmenu

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 2022-08-20
    • 2017-08-19
    相关资源
    最近更新 更多