I want this to loop 3 times but it is not working, please help

+2 Ash Marz · July 5, 2015

from random import *





print("ROCK(0) \t PAPER(1) \t SCISSORS(2)")


choice = eval(input("Enter your choice: "))


if (choice == 0):


    print("You chose ROCK")


elif (choice == 1):


    print("You chose PAPER")


elif (choice == 2):


    print("You chose SCISSORS")





choice1 = randint(0,2)


if (choice1 == 0):


    print("The computer chose ROCK")


elif (choice1 == 1):


    print("The computer chose PAPER")


elif (choice1 == 2):


    print("The computer chose SCISSORS")


count = 0





while count <= 3:
    if choice == 0:
        if choice1 == 0:
            print("Draw")
        if choice1 == 1:
            print("You Lose")
        if choice1 == 2:
            print("You Win") 
        print("ROCK(0) \t PAPER(1) \t SCISSORS(2)") 
        choice = eval(input("Enter your choice: "))
    if  choice == 1:
        if choice1 == 0:
            print("You Win")
        if choice1 == 1:
            print("Draw")
        if choice1 == 2:
            print("You Lose")
    print("ROCK(0) \t PAPER(1) \t SCISSORS(2)") 
    choice = eval(input("Enter your choice: "))
    if choice == 2:
        if choice1 == 0:
            print("You Lose")
        if choice1 == 1:
            print("You Win")
        if choice1 == 2:
            print("Draw")
    print("ROCK(0) \t PAPER(1) \t SCISSORS(2)") 
    choice = eval(input("Enter your choice: "))
    count += 1    
[/code]

[/code]

Post a Reply

Replies

Oldest  Newest  Rating
0 kevin brasher · July 5, 2015
Hey, you will need to use a for loop. A for loop allows you to complete  a set of actions based on a condition. 
0 Chad Batista · July 5, 2015
Here's an idea...


from random import *


count = 0


while count < 3:
    print("ROCK(0) \t PAPER(1) \t SCISSORS(2)")
    choice = eval(input("Enter your choice: "))

    if (choice == 0):
        print("You chose ROCK")
    elif (choice == 1):
        print("You chose PAPER")
    elif (choice == 2):
        print("You chose SCISSORS")

    choice1 = randint(0,2)

    if (choice1 == 0):
        print("The computer chose ROCK")

    elif (choice1 == 1):
        print("The computer chose PAPER")
    elif (choice1 == 2):
        print("The computer chose SCISSORS")

    count = count + 1

I'm sure you can figure out the rest from that.
0 Ash Marz · July 5, 2015
@Chad Batista  

I still cannot get it right
0 Tony Stark · July 5, 2015
My indentation messed up as I pasted it on here but should hopefully not be confusing
'''
Guessing you want to play rock paper scissors three times then if score =- 3 player wins or looses?
'''
import random

# keep score
score = 0
# how many times game has been played - for while loop
played = 0
# easy to store the options in the list than type
options = ['rock', 'paper', 'scissors']
# dictionary to decide who beats who
beats = {'rock': 'scissors', 'paper': 'rock', 'scissors': 'paper'}
# self explanatory
text = 'You chose: '
# variables to keep what choices the player & computer chose for comparing later
PlayerChoose = ''
ComputerChoose = ''
while played < 3:
# print Round 0, Round 1, etc
print("Round ", played)
print("ROCK(0) \t PAPER(1) \t SCISSORS(2)")
# store choise
choice = int(input("Enter your choice: "))
# depending on choice, print what they chose & save it in PlayerChoose variable
if choice == 0:
print(text + options[0])
PlayerChoose = 'rock'
elif choice == 1:
print(text + options[1])
PlayerChoose = 'paper'
elif choice == 2:
print(text + options[2])
PlayerChoose = 'scissors'
# computer's choices
rand = random.randrange(0, 2)
ComputerChoose = options[rand]
print('Computer chose ' + ComputerChoose)
# the smart bit
# for the dictionary, if I do beats[rock] the output is 'scissors' as that's the value
# so if player chooses 'paper' then computer can only beat if it chose 'scissors'
# as beats[scissors] == paper, read through the code and understand how it works
if PlayerChoose == ComputerChoose:
print('Tie!')
elif PlayerChoose == beats[ComputerChoose]:
print('You loose!')
elif ComputerChoose == beats[PlayerChoose]:
print('You win!')
score += 1
played += 1

print('You scored: ', score)
0 Chad Batista · July 6, 2015
Which part, looping it 3 times?
0 Otto Von Chesterfield · July 6, 2015
Here's a few examples of looping:


max_num = 3

#For __ in range(__) loop
for i in range(max_num): #Also range(0, max_num)
  print(i)
  #Code

i = 0
#While i < __, ..., i += 1 loop
while i < max_num:
  #Code
  print(i)
  i += 1

In either case, you should get printed:
0
1
2


Hope this helps!
0 Ebenezer Popoola · July 7, 2015
As @Kevin said, try to use a for loop to test it since you are sure about the number of times you expect the loop to run.
  • 1

Python

107,285 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator