Python program not looping/restarting

166 views Asked by At

I have a school assessment which is to make a child's spelling game, it has to loop/restart when the player clicks yes. So far when I test the game, the option/the easygui.buttonbox that asks the player if they want to play again and the yes/no options to play again or exit the game dose not appear. The program just closes after the player final score is displayed.

Here is my coding for the game, I can't find what I've done wrong and i had tried 3 suggestions to fix the coding and none of them have worked.

import easygui
#Childs Litercay Game
#Charlotte Lowe 03/09/2015

#Declare Constants and Variables
Score = 0
PlayerAnswer = 0
playOn = 0
while playOn != "Yes":
    playOn = easygui.buttonbox ("Hey there, are you ready to test your spelling?", choices = ["Yes"])
if "Yes":
    PlayerName = easygui.enterbox ("Before we begin, what's your name?")

easygui.msgbox ("Hi " + PlayerName +"! Here are the rules of the game.")
easygui.msgbox ("There will be 3 words you can choose from, 2 will be incorrect and 1 will be right.")
easygui.msgbox ("All you have to do is select the correctly spelt word.")
easygui.msgbox ("For every right word you pick you will earn a point! There are 10 questions in this quiz")
easygui.msgbox ("Let's begin!")

PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices = ["Awesome","Awsome","Awesom"])
if PlayerAnswer == "Awesome":
    Score += 1
    easygui.msgbox ("You're right! Your score is " + str(Score))
elif PlayerAnswer == "Awsome":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is " + str(Score))
elif PlayerAnswer == "Awesom":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Becuse","Becus","Because"])
if PlayerAnswer == "Becuse":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Becus":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Because":
    Score += 1
    easygui.msgbox ("Correct! Your score is now " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Morning","Moring","Morening"])
if PlayerAnswer == "Morning":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "Moring":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Morening":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Beleave","Believe","Belive"])
if PlayerAnswer == "Believe":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "Beleave":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Belive":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Jewelry","Jewlery","Jewley"])
if PlayerAnswer == "Jewelry":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "Jewlery":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Jewley":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Mispelled","Misspelled","Misspeled",])
if PlayerAnswer == "Misspelled":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "Mispelled":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Misspeled":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly    
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["acceptable","aceptable","acceptble",])
if PlayerAnswer == "acceptable":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "aceptable":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "acceptble":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["calendar","calender","callendar",])
if PlayerAnswer == "calendar":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "calender":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "callendar":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["equepment","equiptment","equipment",])
if PlayerAnswer == "equipment":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "equiptment":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "equipment":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["library","liebary","libary",])
if PlayerAnswer == "library":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "liebary":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "lieberry":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
if Score == 10:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, you got them all right " + PlayerName+ "!")
elif Score == 9:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, one away " + PlayerName+ "!")
elif Score == 8:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, so close "+ PlayerName+"!")
elif Score == 7:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, nearly there " + PlayerName+"!")
elif Score == 6:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, amlost made it " + PlayerName+ "!")
elif Score == 5:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, half way there " + PlayerName+ "!")
elif Score == 4:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, a little more pratice " + PlayerName+ "!")
elif Score == 3:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, pratice makes perfect "+ PlayerName+"!")
elif Score == 2:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, try again " + PlayerName+"!")
elif Score == 1:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, better luck next time " + PlayerName+ "!")
elif Score == 0:
    easygui.msgbox ("You got a score of " + str(Score) + "out of 10, better luck next time " + PlayerName+ "!")

while playOn != "Yes":
   playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
   if playOn == "Yes":
      Score = 0 #resets score count, if player wants to play again

   elif playOn == "No":
     easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")

I'm hoping someone out in the world wide web can pick up on what I've done wrong. I'm very new to python so any help I can get will be so awesome. I know I have asked for help with this piece of coding before, but I only just made an account today, so I don't know how to reply to any comments or anything!

2

There are 2 answers

1
John1024 On BEST ANSWER

To get it to play again if the user answers yes, you need to put the game in a while loop. With minimal changes to your game, it might look like:

import easygui
#Childs Litercay Game
#Charlotte Lowe 03/09/2015

#Declare Constants and Variables
Score = 0
PlayerAnswer = 0
playOn = 0
while playOn != "Yes":
    playOn = easygui.buttonbox ("Hey there, are you ready to test your spelling?", choices = ["Yes"])
if "Yes":
    PlayerName = easygui.enterbox ("Before we begin, what's your name?")

easygui.msgbox ("Hi " + PlayerName +"! Here are the rules of the game.")
easygui.msgbox ("There will be 3 words you can choose from, 2 will be incorrect and 1 will be right.")
easygui.msgbox ("All you have to do is select the correctly spelt word.")
easygui.msgbox ("For every right word you pick you will earn a point! There are 10 questions in this quiz")
easygui.msgbox ("Let's begin!")

while True:
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices = ["Awesome","Awsome","Awesom"])
    if PlayerAnswer == "Awesome":
        Score += 1
        easygui.msgbox ("You're right! Your score is " + str(Score))
    elif PlayerAnswer == "Awsome":
            Score += 0
            easygui.msgbox ("Sorry Incorrect! Your score is " + str(Score))
    elif PlayerAnswer == "Awesom":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Becuse","Becus","Because"])
    if PlayerAnswer == "Becuse":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Becus":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Because":
        Score += 1
        easygui.msgbox ("Correct! Your score is now " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Morning","Moring","Morening"])
    if PlayerAnswer == "Morning":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "Moring":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Morening":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Beleave","Believe","Belive"])
    if PlayerAnswer == "Believe":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "Beleave":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Belive":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Jewelry","Jewlery","Jewley"])
    if PlayerAnswer == "Jewelry":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "Jewlery":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Jewley":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Mispelled","Misspelled","Misspeled",])
    if PlayerAnswer == "Misspelled":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "Mispelled":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Misspeled":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly____
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["acceptable","aceptable","acceptble",])
    if PlayerAnswer == "acceptable":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "aceptable":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "acceptble":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["calendar","calender","callendar",])
    if PlayerAnswer == "calendar":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "calender":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "callendar":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["equepment","equiptment","equipment",])
    if PlayerAnswer == "equipment":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "equiptment":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "equipment":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["library","liebary","libary",])
    if PlayerAnswer == "library":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "liebary":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "lieberry":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    if Score == 10:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, you got them all right " + PlayerName+ "!")
    elif Score == 9:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, one away " + PlayerName+ "!")
    elif Score == 8:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, so close "+ PlayerName+"!")
    elif Score == 7:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, nearly there " + PlayerName+"!")
    elif Score == 6:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, amlost made it " + PlayerName+ "!")
    elif Score == 5:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, half way there " + PlayerName+ "!")
    elif Score == 4:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, a little more pratice " + PlayerName+ "!")
    elif Score == 3:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, pratice makes perfect "+ PlayerName+"!")
    elif Score == 2:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, try again " + PlayerName+"!")
    elif Score == 1:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, better luck next time " + PlayerName+ "!")
    elif Score == 0:
        easygui.msgbox ("You got a score of " + str(Score) + "out of 10, better luck next time " + PlayerName+ "!")

    playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
    if playOn == "Yes":
       Score = 0 #resets score count, if player wants to play again
    elif playOn == "No":
      easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")
      break

A simpler form

Computers are better at repetitive things than humans. The code below is modified to take advantage of that:

import easygui
#Childs Litercay Game
#Charlotte Lowe 03/09/2015

def play(choices, answer, Score):
    """Check spelling."""
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices=choices)
    if PlayerAnswer == choices[answer]:
        Score += 1
        easygui.msgbox ("You're right! Your score is " + str(Score))
    else:
        easygui.msgbox ("Sorry Incorrect! Your score is " + str(Score))
    return Score
#    print(Score) #To check if programme is calculating score properly

tests = (
        (["Awesome","Awsome","Awesom"], 0),
        (["Becuse","Becus","Because"], 2),
        (["Morning","Moring","Morening"], 0),
        (["Beleave","Believe","Belive"], 1),
        (["Jewelry","Jewlery","Jewley"], 0),
        # Fill in the rest.....
    )

congrats = {
        10:"you got them all right ",
        9:"one away",
        8:"so close",
        7:"nearly there",
        6:"amlost made it",
        5:"half way there",
        4:"a little more pratice",
        3:"pratice makes perfect",
        2:"try again",
        1:"better luck next time",
        0:"better luck next time",
}

# Hello and instructions
playOn = easygui.buttonbox ("Hey there, are you ready to test your spelling?", choices = ["Yes"])
PlayerName = easygui.enterbox ("Before we begin, what's your name?")
easygui.msgbox ("Hi " + PlayerName +"! Here are the rules of the game.")
easygui.msgbox ("There will be 3 words you can choose from, 2 will be incorrect and 1 will be right.")
easygui.msgbox ("All you have to do is select the correctly spelt word.")
easygui.msgbox ("For every right word you pick you will earn a point! There are 10 questions in this quiz")
easygui.msgbox ("Let's begin!")

while True:
    Score = 0
    for choices, answer in tests:
        Score = play(choices, answer, Score)
        print(Score) #To check if programme is calculating score properly
    easygui.msgbox (congrats[Score] +" " + PlayerName+ "!")

    playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
    if playOn == "No":
      easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")
      break

Notes:

  • The code for giving the user three choices and updating the score now appears once in a function that is called many times for each set of choices.

  • The code summing up the users performance is reduced to a few line which get the words of encouragement from a dictionary.

0
Andy On

You are setting playOn to "Yes" in your first loop.

while playOn != "Yes":
    playOn = easygui.buttonbox ("Hey there, are you ready to test your spelling?", choices = ["Yes"])
if "Yes":
    PlayerName = easygui.enterbox ("Before we begin, what's your name?")

At the end of the program, you are going into a while loop only while playOn does not equal "Yes".

while playOn != "Yes":
   playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
   if playOn == "Yes":
      Score = 0 #resets score count, if player wants to play again

   elif playOn == "No":
     easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")

But, you set (and did not change) playOn at the beginning of your program to "Yes", so the loop is skipped and the program ends.


A few notes to help you out

  • "amlost made it"/"little more pratice"/"pratice makes perfect" - You have some spelling errors in your spell checker program.
  • The program is very verbose and you have a lot of redundant code. You mentioned this was a school assignment so I'm not going to provide shorter code samples, but I'd encourage you to look up functions.
  • Even fixing the main issue with playOn, clicking "Yes" isn't going to reset your program. You still need a way to get to the beginning of your spell check code (hint: functions again are your friend/alternatively an outer loop will also help)