Home > Software engineering >  I am completely lost in how to make my program loop
I am completely lost in how to make my program loop

Time:10-19

This is a rock paper scissors program and I need help making it loop at least three times. Here is the code if you need it.

import random

#welcome
Welcome = input("welcome please choose the following options (press enter)")
while True:
#Options (Rock, Paper, Scissor) Make sure to use capital letters
player1 = input("Rock, Paper, Scissors : ")
player2 = random.choice(["Rock", "Paper", "Scissors"])
print("Player 2 selected: ", player2)

#Calculating Win/Lose
if player1 == "Rock" and player2 == "Paper":
    print("Player 2 Won")
elif player1 == "Paper" and player2 == "Scissor":
    print("Player 2 Won")
elif player1 == "Scissor" and player2 == "Rock":
    print("Player 2 Won")
elif player1 == player2:
    print("Tie") 
else:
    print("Player 1 Won")
  
  while True:
    if input('Do you want to repeat(y/n)') == 'n':
        break 

CodePudding user response:

You can add a for loop inside your code,

import random

#welcome
Welcome = input("welcome please choose the following options (press enter)")
play = True
while play:
    for _ in range(3):
        print(f"Round : {_ 1}")
        player1 = input("Rock, Paper, Scissors : ")
        player2 = random.choice(["Rock", "Paper", "Scissors"])
        print("Player 2 selected: ", player2)

        
        if player1 == "Rock" and player2 == "Paper":
            print("Player 2 Won")
        elif player1 == "Paper" and player2 == "Scissor":
            print("Player 2 Won")
        elif player1 == "Scissor" and player2 == "Rock":
            print("Player 2 Won")
        elif player1 == player2:
            print("Tie") 
        else:
            print("Player 1 Won")
  
    if input('Do you want to repeat(y/n)') == 'n':
        play = False # Exit your main while loop with a flag

CodePudding user response:

your second while loop does not go to the next game iteration if the input is "y" (yes), nor do you exit the outer most while loop when the input is "n".

  • Related