I have a program that scores a Scrabble game, and the main()
function at the end of the program calls the functions needed depending on user input.
def main():
option = home()
if option.lower() == "a":
add_score()
elif option.lower() == "v":
view_scores()
elif option.lower() == "u":
undo()
elif option.lower() == "end":
end_game()
else:
print("Please enter a valid option.")
main()
At the end of the program, there is an if function to check if it is being used as an import.
if __name__ == "__main__":
main()
There is also the function end_game()
, which prints scores and is supposed to end the game.
def end_game():
for name, score in players:
print("Player %s has a score of %d" % (name, score))
The problem occurs when after the user types "end" and the end_game()
function executes. After end game executes it runs into the if function checking for an import at the end of the program which triggers main again. Basically, the user gets stuck in a loop that could only be exited using CTRL+C. Would setting name to a different value such as "stop" be a valid way to bypass the if statement. I have not seen people changing name before in code, so I'm not sure if it would be a valid way to stoop the if statement from executing.