0

I have a y/n query at the end of the game which will restart the game or exit the game.

y and n work as they should, but if I enter any other letter even if it does print the "please try again message", it restarts the game anyway. I thought it should only do that if I put continue, but I haven't

I also want it to recognize y or n as upper case, is there a command in python that allows a letter to be recognized regardless of if its upper or lowercase?

    play_again = input('Would you like to play again?(y/n) ') 
    if play_again == 'y': #loop restarts
        print('Starting another game...')
        continue 
    elif play_again == 'n': #loop is exited
        print('Thank you for playing! You have now exited the game.')
        break
    else:
        print ("I don't recognize that character, please select y or n")

I have provided the loop as requested, but the last time I did this I got in trouble:

    number_of_stones = int(input('Select between 30-50 stones to start your game: ' )) #player1 chooses stones
    if number_of_stones >= 30 and number_of_stones <= 50:
        print('Starting the game...')      
        has_winner = False   # Winner = False since there no winners (start of game)
        while True:
            for player in player_list:                                
                if has_winner: # if there is a winner, exit loop
                    break
                # display whose turn is it
                print('\n{}\'s turn:'.format(player))
                while True:
                    # if the player is computer use strategy that is mentioned in assignment 2
                    if player == 'Mr Computer':
                        remainder = number_of_stones%3
                        if remainder == 0:
                            stones_to_remove = 2
                        else:
                            stones_to_remove = 1
                    # if the player is not the computer
                    else:
                        stones_to_remove = int(input('Select between 1-3 stones to remove: ')) 

                    # assess the number of stones remaining and print the remainder to the screen
                    if stones_to_remove >= 1 and stones_to_remove <= 3:
                        number_of_stones -= stones_to_remove
                        print('Number of stones remaining:',number_of_stones)
                        if number_of_stones <= 0:
                            print('The winner is:',player,"!")
                            has_winner = True    
                        break
                    
                    else: # show error and let the user input again
                        print("I'm sorry, that number is outside the range. Please choose a number between 1 and 3")            
            if has_winner == True: # if the winner=true then exit loop
                break
        play_again = input('Would you like to play again?(y/n) ').lower() #only accepts lower case letter
        if play_again == 'y': #loop restarts
            os.system('cls') # clear the console
            print('Starting another game...')
            continue 
        elif play_again == 'n': #loop is exited
            print('Thank you for playing! You have now exited the game.')
            break
    else:
        print("I'm sorry, that number is outside the range. Please select between 30 and 50 stones.")
CWoolford
  • 15
  • 1
  • 3
  • Can you show the whole code block from the beginning of the loop? Also for the recognision, you can use the functions `upper()` or `lower()` to make the case regardless – Zaid Al Shattle Oct 12 '21 at 08:31
  • to check for either case use .lower(), `if play_again.lower() == 'y'` will return True for 'y' and 'Y' – Patrick Oct 12 '21 at 08:32

1 Answers1

0

You haven't added necessary information ,i.e. rest of the loop , try add that . For case insensitivity , just use the following

if play_again == "n" || play_again == "N":

.