Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can we delete a failed round from range in rock/paper/scissor?

I have written a code for this game and I have written range for it that do this code for 5 times and then ask about our decision that if we want to continue or not but when I run it in terminal and write another word except s,p,r it counts this round too and the game will run for 4 times.


from random import randint
game_choices = {
    'r': "rock",
    'p': "paper",
    's': 'scissor'
}
player_score = 0
pc_score = 0

for i in range(5):  
    player = input('please enter your choose from s(scissor),r(rock),p(paper):')
    if player not in ['s', 'r', 'p']:
        print('not a valid input!')

    print('your choice is ' + game_choices[player])
    choices = ['s', 'r', 'p']
    random_number = randint (0, 2)
    pc = choices[random_number]
    print('computer choice is ' + game_choices[pc])
    if player == pc:
        print('_-_-_-_')
        print('equaled')
    elif player == 's':
        if pc == 'r':
            print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
            print("!computer win! score it's for computer!")
            pc_score += 1
        else:
            print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
            print("!you win! score it's for you!")
            player_score += 1
    elif player == 'r':
        if pc == 'p':
            print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
            print("!computer win! score it's for computer!")
            pc_score += 1
        else:
            print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
            print("!you win! score it's for you!")
            player_score += 1
    elif player == 'p':
        if pc == 's':
            print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
            print("!computer win! score it's for computer!")
            pc_score += 1
        else:
            print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
            print("!you win! score it's for you!")
            player_score += 1
    print('---------------------------------------')
    print("your total score:" + str(player_score))
    print('....................')
    print('computer total score:' + str(pc_score))
    print('---------------------------------------')
if player_score == pc_score:
    print("no one win")
elif player_score > pc_score:
    print("!congratulation! totally you are the winner")
else:
    print("totally pc is the winner")
while True:
    game = input('do you want to play again? (y/n):')
    if game == 'y':
        pass
    else:
        break

I would appreciate you if you can help me in this problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One way to address this issue is to include the round counting inside a while loop and only increment the round counter when a valid input is provided. Here’s the modified code with this adjustment

    from random import randint

game_choices = {
    'r': "rock",
    'p': "paper",
    's': 'scissor'
}
player_score = 0
pc_score = 0
rounds_played = 0
total_rounds = 5

while rounds_played < total_rounds:
    player = input('Please enter your choice from s (scissor), r (rock), p (paper): ')

    if player not in ['s', 'r', 'p']:
        print('Not a valid input!')
        continue  # Skip the rest of the loop and restart

    print('Your choice is ' + game_choices[player])

    choices = ['s', 'r', 'p']
    random_number = randint(0, 2)
    pc = choices[random_number]
    print('Computer choice is ' + game_choices[pc])

    if player == pc:
        print('-------------------------')
        print('Equal!')
    elif player == 's':
        if pc == 'r':
            print('-------------------------')
            print("! Computer wins! Score goes to computer!")
            pc_score += 1
        else:
            print('-------------------------')
            print("! You win! Score goes to you!")
            player_score += 1
    elif player == 'r':
        if pc == 'p':
            print('-------------------------')
            print("! Computer wins! Score goes to computer!")
            pc_score += 1
        else:
            print('-------------------------')
            print("! You win! Score goes to you!")
            player_score += 1
    elif player == 'p':
        if pc == 's':
            print('-------------------------')
            print("! Computer wins! Score goes to computer!")
            pc_score += 1
        else:
            print('-------------------------')
            print("! You win! Score goes to you!")
            player_score += 1

    rounds_played += 1  # Increment the round counter
    print('---------------------------------------')
    print("Your total score: " + str(player_score))
    print('....................')
    print('Computer total score: ' + str(pc_score))
    print('---------------------------------------')

if player_score == pc_score:
    print("No one wins!")
elif player_score > pc_score:
    print("! Congratulations! You are the winner!")
else:
    print("The computer is the winner!")

while True:
    game = input('Do you want to play again? (y/n): ')
    if game == 'y':
        break  # Break out of this loop to restart the game
    elif game == 'n':
        break  # Break out of the outer loop to exit the game
    else:
        print('Invalid input! Please enter y (yes) or n (no).')

This way, if the player enters an invalid input, it will prompt them again for a valid input without counting that round towards the total rounds played.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading