Python Forum
My program won't run through the entire code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My program won't run through the entire code
#1
I have created a program that plays rock, paper, scissors; keeps track of statistics including wins, losses, and ties; and is able to save and load file. My program is able to print the opening lines of code that I have, but ends regardless of what value is entered by the user. How can I get the program to continue while retaining my while(true) loop to validate input? Here is my code. The problem is with the variable choice, but I believe I probably have the same problem with the variables user_choice and user_choice_2 variables. Thanks for the help.
# This program allows you to play rock, paper, scissors against the computer
import random

# Global constants for menu choices
START_NEW_GAME = 1
LOAD_GAME= 2
QUIT = 3

#Global constants for statistics
Wins=0
Losses=0
Ties=0

# main function
def main():
    print('Welcome to Rock, Paper, Scissors!')
    

    print('1. Start New Game')
    print('2. Load Game')
    print('3. Quit')

    #Initialize a variable for the user's choice.
    choice=0
    
    

    # process menu selections until the user wants to quit the program.

    #Get the user's menu choice.
    while(True):
        try:
            choice = int(input("Enter your choice: "))
            if choice >= START_NEW_GAME and choice <= QUIT:
                break
        except ValueError:
            print("Invalid input!")
        else:
            break

        if choice==1:
            start_new_game()
        if choice==2:
            load_game()
        if choice==3:
            break

# The start_new_game function allows a player to start a new game
def start_new_game():
    name_of_player = input('What is your name? ')
    print("Hello " + name_of_player + "." + " Let's play!")

def load_game():
    while(True):
        try:
            name_of_player = input('What is your name? ')
            infile = open(name_of_player + '.txt', 'r')
            #read three lines from the file displays wins, losses, ties.
            line1 = infile.readline()
            line2 = infile.readline()
            line3 = infile.readline()
            line4 = infile.readline()

            #close the file
            infile.close()

            #print the data that was read into memory
            Wins = line2
            Losses = line3
            Ties = line4
        except:
            print(name_of_player + ',' +' your game could not be found')
            main()
        else:
            print("Welcome back" + name_of_player + "." + " Let's play!")
            break

# Global constants for menu choices
ROCK = 1
PAPER = 2
SCISSORS = 3
Round = Wins+Losses+Ties
# The game_menu_choice function displays the gameplay menu
# and gets a validated choice from the user
def game_menu_choice():
    print()
    print("Round", Round)

    print('1. Rock')
    print('2. Paper')
    print('3. Scissors')
    print('What will it be?')
    
    print()

    # Get the user's choice.
    user_choice = int(input('Enter your choice: '))
    # Validate the choice.
    # return the user's choice
    while(True):
        try:
            user_choice = int(input("Enter your choice: "))
            if user_choice >= ROCK and choice <= SCISSORS:
                break
        except ValueError:
            print("Invalid input!")
        else:
            break

 
        
    
    
    
# This program will allow the user to actually play rock, paper, scissors
def play_the_game():
    computer_choice = random.randint(1,3)
    if computer_choice==1:
        computer_choice_rock()
    elif computer_choice==2:
        computer_choice_paper()
    else:
        computer_choice_scissors()
def computer_choice_rock():
    if user_choice=="1":
        print("You chose Rock. The computer chose Rock. You tie!")
        Ties+=1
        try_again()
    if user_choice=="2":
        print("You chose Paper. The computer chose Rock. You win!")
        Wins+=1
        try_again()
    if user_choice=="3":
        print("You chose Scissors. The computer chose Rock. You lose!")
        Losses+=1
        try_again()
    else:
        print("You entered an invalid value. Please try again and enter either 1,2, or 3")
        computer_choice_rock()

def computer_choice_paper():
    if user_choice=="1":
        print("You chose Rock. The computer chose Paper. You lose!")
        Losses+=1
        try_again()
    if user_choice=="2":
        print("You chose Paper. The computer chose Paper. You tie!")
        Ties+=1
        try_again()
    if user_choice=="3":
        print("You chose Scissors. The computer chose Paper. You win!")
        Wins+=1
        try_again()
    else:
        print("You entered an invalid value. Please try again and enter either 1,2, or 3")
        computer_choice_paper()
def computer_choice_scissors():
    if user_choice=="1":
        print("You chose Rock. The computer chose Scissors. You win!")
        Wins+=1
        try_again()
    if user_choice=="2":
        print("You chose Paper. The computer chose Scissors. You lose!")
        Losses+=1
        try_again()
    if user_choice=="3":
        print("You chose Scissors. The computer chose Scissors. You tie!")
        Ties+=1
        try_again()
    else:
        print("You entered an invalid value. Please try again and enter either 1,2, or 3")
        computer_choice_scissors()

# Global constants for try_again menu choice
PLAY_AGAIN = 1
VIEW_STATISTICS = 2
QUIT = 3        
# Define Try again Function
def try_again():
    print()
    print("What would you like to do?")

    print("1. Play again")
    print("2. View Statistics")
    print("3. Quit")

    print("Enter choice: ")
    print()


    # Get the user's choice.
    user_choice_2 = int(input('Enter your choice: '))
    # Validate the choice.
    while(True):
        try:
            user_choice_2 = int(input("Enter your choice: "))
            if user_choice_2 >= PLAY_AGAIN and user_choice_2 <= QUIT:
                break
        except ValueError:
            print("Invalid input!")
        else:
            break
       

    if user_choice_2=="1":
        game_menu_choice()
    if user_choice_2=="2":
        statistics()
    if user_choice_3=="3":
        write_statistics()
def statistics():
    print(name_of_player, + ',' + 'here are your game play statistics...')
    print('Wins:', Wins)
    print('Losses:', Losses)
    print('Ties:', Ties)
    ratio = Wins/Losses
    print('Win/Loss Ratio: ', format(ratio, '.2f'))

    try_again()

def write_statistics():
    #This writes four lines of data which are name of user, wins, losses, and ties
    outfile = open(name_of_player + '.txt','w')

    outfile.write(name_of_player + '\n')
    outfile.write(Wins + '\n')
    outfile.write(Losses + '\n')
    outfile.write(Ties + '\n')

    #close the file
    outfile.close()
    

main()
Reply
#2
The problem is line 34. Well, the first problem is line 34:

            if choice >= START_NEW_GAME and choice <= QUIT:
                break
This code says that if the input is valid (that's the condition), break out of the while loop (which ends the game). I don't know why you have a break there. You can't break out of a try block, you only break out of loops.

When you are trying to get valid input, only the validation goes in the loop.

while True:  # Note that you don't need parentheses here.
    try:
        choice = int(input('Enter a number from 1 to 10: '))
    except ValueError:
        continue  # Keep trying to get valid input.
    if 1 <= choice <= 10:  # Valid input
        break  # Stop trying to get input
print(choice * 10)  # Do something with the input.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help Me Code This Program NZedMarine 1 2,080 Nov-16-2018, 12:11 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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