1

I have written logic in python for the game of rock, paper , scissor but having trouble to get it to run. It wont run the through the entire game and just keeps asking me for my input. I have looked through it multiple times but cant see where it might be getting stuck. Any idea what the issue may be?

Code is as below:

import random

comp_wins = 0
player_wins = 0


def choose_option ():
  user_choice = input("choose rock, paper and scissors: ")
  if user_choice in ["rock","Rock"]:
    user_choice = "rock"
  elif user_choice in ["paper","Paper"]:
    user_choice = "paper"
  elif user_choice in ["scissors","Scissor"]:
    user_choice = "scissors"
  else:
      print ("error try again")
  return user_choice

def computer_option ():
    comp_choice = random.randint(1,3)
    if comp_choice ==1:
          comp_choice = "rock"
    elif comp_choice ==2:
          comp_choice = "paper" 
    elif comp_choice ==3:
        comp_choice = "scissors"
    return comp_choice

  
while True:
    print("")
    user_choice = choose_option ()
    comp_choice = computer_option ()
    print ("")

if user_choice ==["rock"]:
    if comp_choice == "rock":
        print("draw")

    elif comp_choice == "paper":
        print("computer wins")
        comp_wins += 1

    elif comp_choice == "scissors":
        print("user wins")
        player_wins += 1
        

elif user_choice == "paper":
    if comp_choice == "rock":
        print("user wins")
        player_wins += 1

    elif comp_choice == "paper":
        print("draw")

    elif comp_choice == "scissors":
        print("computer wins")
        comp_wins += 1

elif user_choice == "scissors":
    if comp_choice == "rock":
        print("computer wins")
        comp_wins += 1

    elif comp_choice == "paper":
        print("user wins")
        player_wins += 1

    elif comp_choice == "scissors":
        print("draw")

print("")
print("user_wins: " + str(player_wins)) 
print("comp_wins: " + str(comp_wins))
print("")
1
  • 2
    It's because of the way you wrote the loop. A while True loop is supposed to go on forever. You'll need to indent the big if-else block inside the loop. Commented Apr 28, 2021 at 17:02

3 Answers 3

2

Youur issue is here

while True:
print("")
user_choice = choose_option ()
comp_choice = computer_option ()
print ("")

You never exit this while, you should put an exit condition or a flag when a choice has been made. Also it would be better to clean up and use a Top-Down structure with a main function in which you control the file.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks that there is a wrong indent: the main while True loop never ends. Try to move all blocks after this loop to the right (i.e. add four spaces in the beginning of all next lines).

Comments

1

Your indentation levels aren't correct. The if user_choice ==["rock"]: part is the same indentation level as the while statement, so it isn't included in the loop. You need to indent it to let Python know it's part of the loop block. And since you don't have anything that ends the loop, it will go on forever. Also, using strings to represent choices is really inefficient. If you represent choices with numbers, you can use modular arithmetic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.