Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Breaking down long function
#1
Hey there. 
I need some help and guidance in breaking down a long function but can seem to grasp exactly how....:
Any help would  be greatly appreciated!! :) Onelove!  Heart
def play_game(): 
    level = get_level() 
    quiz = game_data[level]['quiz'] 
    print quiz
    answers_list =  game_data[level]['answers']
    blanks_index = 0 
    game_over = 0 
    answers_index = 0 
    guesses = 3 


    
    while blanks_index < len(blanks):
        user_answer = raw_input("So what's your answer to question " + blanks[blanks_index] + "? : ")
        if check_answer(user_answer,answers_list,answers_index) == "right_answer":
            print "nice job! that is the right answer!\n"
            quiz = quiz.replace(blanks[blanks_index], user_answer.upper())
            blanks_index += 1 
            answers_index += 1 
            guesses = 3
            print quiz
            if blanks_index == len(blanks): 
                time.sleep (3) 
        else: 
            guesses -= 1 
            if guesses == game_over: 
                print "Wawaooohh, you lost..... :("
                time.sleep (3)
                break
            else:
                print "please try again. one guess down."
Reply
#2
This is a bit convoluted. Could you please show blanks
Reply
#3
Hey sorry, It's quite a long piece of code.
Essentially I have helper functions:
get_level: User inputs raw_input
check_answer: confirms from my dictionary list, answers and returns.. If correct or not...

blanks = ["__1__","__2__","__3__", "__4__"]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 299 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Openpyxl module breaking my code EnderSM 5 1,053 May-26-2023, 07:26 PM
Last Post: snippsat
  breaking out of nested loops Skaperen 3 1,213 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to use += after breaking for loop? Frankduc 2 1,483 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  How to fix while loop breaking off raphy11574 3 2,179 Oct-12-2021, 12:56 PM
Last Post: deanhystad
  breaking a large program into functions, not acting as expected Zane217 9 3,010 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,357 Sep-04-2021, 04:51 PM
Last Post: imillz
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,548 Jan-11-2021, 07:42 AM
Last Post: kamui123
  Run a timer when a functions starts to see how long the function takes to complete Pedroski55 2 1,995 Apr-19-2020, 06:28 AM
Last Post: Pedroski55
  breaking outof nexted loops Skaperen 4 3,110 Feb-07-2020, 02:04 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020