# Loops in my game

I made a rock paper scissors game and i want to add a loop that loops the code 5 times, but i don’t know how.
Code below:

``````import random
player = int(input("1-scissors, 2-paper, 3-rock"))
komp = random.randint(1, 3)
print(player)
print(komp)
scoregracz = 0
scorekomp = 0

if player == komp:
print("draw")
elif player == 2 or komp == 3:
scorekomp = scorekomp + 1
print("you lost")
elif player == 1 or komp == 3:
scoregracz = scoregracz + 1
print("you won")
elif player == 1 or komp == 2:
scorekomp = scorekomp + 1
print("you lost")
elif player == 2 or komp == 1:
scoregracz = scoregracz + 1
print("you won")
else:
print("wrong choice")

print ("you have " , scoregracz, " points" , " : " , " komp has " , scorekomp , ".")
``````

To add a loop that runs the code 5 times, you can use a for loop. Here’s an example of how to modify your code to run the game 5 times:

``````import random

score_gracz = 0
score_komp = 0

for i in range(5):
print("Round ", i+1)
player = int(input("1-scissors, 2-paper, 3-rock "))
komp = random.randint(1, 3)
print("Player: ", player)
print("Komp: ", komp)

if player == komp:
print("Draw")
elif player == 2 and komp == 3:
score_komp += 1
print("You lost")
elif player == 1 and komp == 3:
score_gracz += 1
print("You won")
elif player == 1 and komp == 2:
score_komp += 1
print("You lost")
elif player == 2 and komp == 1:
score_gracz += 1
print("You won")
elif player == 3 and komp == 1:
score_komp += 1
print("You lost")
elif player == 3 and komp == 2:
score_gracz += 1
print("You won")
else:
print("Wrong choice")

print("You have", score_gracz, "points:", "Komp has", score_komp, ".\n")

print("Final score: You have", score_gracz, "points. Komp has", score_komp, "points.")
``````

In the modified code, the `for` loop runs the game 5 times. At the end of each round, the scores are updated and printed. After 5 rounds, the final score is printed. Note that I also made some modifications to your original code to fix some logical errors in the game rules.

1 Like

Look into `for` and/or `while` loops (This kinda looks like HW, which I’ve seen warnings on before)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.