0

My problem here is when I ask if the player wants to play again, if they say no, I have it to where it's supposed to quit the script. but it doesn't seem to be working. Here's the full code:

def choose_level():

    print("Please choose a level:\n"
          "\n"
          "(Level 1) - Easy\n"
          "(Level 2) - Medium\n"
          "(Level 3) - Hard\n")

    lvl_1={"Level 1","level 1","1"}
    lvl_2={"Level 2","level 2","2"}
    lvl_3={"Level 3","level 3","3"}

    choice=input("")
    if choice in lvl_1:
        level_1()
    elif choice in lvl_2:
        level_2()
    else:
        print ("[!] UNKNOWN LEVEL [!]\n")
        choose_level()

def level_1():

    import random #imports the module 'random'

    yes={"Yes","Y","yes","y"} #set 'yes' to these 4 strings
    no={"No","N","no","n"} #set 'no' to these 4 strings

    name=input("What's your name?\n") #asks for a name

    secret=int(random.random()*10)+1 #randomly generate a number

    trynum=0 #sets the number of tries to 0

    print ("---------------------------------------------------------------------\n"
           "---- Welcome,",name+"! To Level 1!\n"
           "---- I am thinking of a number between 1 and 10.\n"
           "---- Lets see how many times it will take you to guess the number.\n"
           "---------------------------------------------------------------------\n")

    while True: #starts a loop

        trynum=trynum+1 #sets number of tries to itself + 1

        guess=int(input("What is guess #"+str(trynum)+"?\n")) #asks for a guess

        if guess<secret: #if guess is too low/less than(<)
            print ("Too Low. Try again!")

        elif guess>secret: #if guess is too high/greater than(>)
            print ("Oops!! Too high, better luck next try!")

        else: 
            if trynum==1: #if number of tries is only 1
                print ("-------------------------------------------------\n"
                       "----",name+"! You got it in",trynum,"guess!\n"
                       "-------------------------------------------------\n")
            else: #if number of tries is anything else
                print ("-------------------------------------------------\n"
                       "----",name+"! You got it in",trynum,"guesses!\n"
                       "-------------------------------------------------\n")

            if trynum<3: #if guessed in less than 3 tries
                print ("Bravo!")

            elif trynum<5: #if guessed in less than 5 tries
                print ("Hmmmmpf... Better try again")

            elif trynum<7: #if guessed in less than 7 tries
                print ("Better find something else to do!")

            else: #if guessed in more than 7 tries
                print ("You should be embarssed! My dog could pick better.")

            choice=input("""Would you like to play again? ("yes" or "no")\n""") #asks if you want to play again
            if choice in yes: #if yes, then run game again
                choose_level()
            if choice in no: #if no, then quit the game
                print ("Okay.. Goodbye :(")
                quit
            break

def level_2():

    import random

    yes={"Yes","yes","Y","y"}
    no={"No","no","N","n"}

    name=input("What's your name?\n")

    secret=int(random.random()*8)+1

    trynum=0

    print ("--------------------------------------------------------------------------\n"
           "---- Welcome,",name+"! to Level 2!\n"
           "---- I am thinking of a number between 1 and 8.\n"
           "---- You have 20 guesses, and the number changes after every 4th guess\n"
           "--------------------------------------------------------------------------\n")

    while True:

        trynum=trynum+1

        guess=int(input("What is guess #"+str(trynum)+"?\n"))

        if trynum==4:
            secret=int(random.random()*8)+1
            print ("The number has changed!!")

        elif trynum==8:
            secret=int(random.random()*8)+1
            print ("The number has changed!!")

        elif trynum==12:
            secret=int(random.random()*8)+1
            print ("The number has changed!!")

        elif trynum==16:
            secret=int(random.random()*8)+1
            print ("The number has changed!!")

        elif trynum==20:
            print ("-------------------------------------------------\n"
                   "----",name+", you lost! :(\n"
                   "-------------------------------------------------\n")
            choice=input("""Would you like to play again? ("yes" or "no")\n""") #asks if you want to play again
            if choice in yes: #if yes, then run game again
                choose_level()
            elif choice in no: #if no, then quit the game
                print ("Okay.. Goodbye :(")
                quit

        if guess<secret:
            print ("Too low, try again!")

        elif guess>secret:
            print ("Too high, try again!")

        else: 
            if trynum==1: #if number of tries is only 1
                print ("-------------------------------------------------\n"
                       "----",name+"! You got it in",trynum,"guess!\n"
                       "-------------------------------------------------\n")

            else: #if number of tries is anything else
                print ("-------------------------------------------------\n"
                       "----",name+"! You got it in",trynum,"guesses!\n"
                       "-------------------------------------------------\n")

            if trynum<3: #if guessed in less than 3 tries
                print ("Bravo!")

            elif trynum<5: #if guessed in less than 5 tries
                print ("Hmmmmpf... Better try again")

            elif trynum<7: #if guessed in less than 7 tries
                print ("Better find something else to do!")

            else: #if guessed in more than 7 tries
                print ("You should be embarssed! My dog could pick better.")


            choice=input("""Would you like to play again? ("yes" or "no")\n""") #asks if you want to play again
            if choice in yes: #if yes, then run game again
                choose_level()
            if choice in no: #if no, then quit the game
                print ("Okay.. Goodbye :(")
                quit
            break
choose_level()
level_1()
level_2()

The problem I'm having is here:

    choice=input("""Would you like to play again? ("yes" or "no")\n""") #asks if you want to play again
    if choice in yes: #if yes, then run game again
        choose_level()
    if choice in no: #if no, then quit the game
        print ("Okay.. Goodbye :(")
        quit
    break

When I run the program and I get to that point, and type 'no' it just puts me back on the first level. I'm stuck and not sure where to go from here. Any and all help is appreciated.

Jeremiah Ray
  • 9
  • 1
  • 2

2 Answers2

1

You are missing the pair of brackets that come after the quit command. The quit command is calling a function just like choose_level does, so you need to tell Python what parameters you want to pass in. Every function call needs a pair of brackets telling Python what arguments you want. The quit function is no different. In this case, there will be no parameters so you would just type quit().

Avi Caspe
  • 537
  • 4
  • 12
0

sys.exit() , exit() , quit() , and os._exit(0) kill the Python interpreter You should use quit() , not quit