-1

well, I was writing my rock paper scissor code in python and everything was good until when I was trying so much to make it more sophisticated. Till line 28 everything is good and like a usual rock paper scissor game but after that, I wanted to say that we have only 2 right answers for the question "do you want to continue or not" and whenever the answer is yes return the whole game and repeat it (btw because of the return code I add a def and called it RPS) and if the answer of the user is no break it and say goodbye but when I run the code the only thing I receive in the terminal is a "click anything to continue". Please if you know the answer help me. :)

HERE IS MY CODE :

import random
def RPS():
 while True:
  user_action = input("Please enter rock, paper or scissor : ")
  possible_actions = ["rock" , "paper" , "scissor"]
  computer_action = random.choice(possible_actions)

  if user_action == computer_action:
     print(f"Your choice is same as your computer . . . It's a tie!!!")
    
  elif user_action == "rock":
     if computer_action == "scissor":
        print(f"You Won!!! your choice was rock and the computer choice was scissor")
     else:
        print("Paper covers rock! You lose.")

  elif user_action == "paper":
     if computer_action == "rock":
        print(f"You won!!! your choice was rock and the computer choice was rock")
     else:
        print("Scissors cuts paper! You lose.")

  elif user_action == "scissor":
     if computer_action == "paper":
        print(f"You won!!! your choice was scissor and the computer choice was paper")
     else:
        print("Rock smashes scissor. You lose!!!")
  play_again = input("Play again? (y/n): ")

  possible_responses = {'y' , 'n'}
  while possible_responses == play_again.lower():
   if play_again.lower()=='y':
      return(RPS)
   else:
    break
  print(f"Alright. good bye")
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • You also have a problem with your Caps lock key, which I've corrected. Please don't make your entire title all-caps. All-caps is used for emphasis, akin to shouting in real life, and it's somewhat obnoxious when the whole title is formatted that way. – Silvio Mayolo Oct 22 '22 at 00:52
  • 1
    instead of `return(RPS)` it should be `RPS()`. The first one is returning a function callable (like a pointer to the function object but not running that function), while the second one is actually running the function. – Andrew Ryan Oct 22 '22 at 00:57
  • 5
    @AndrewRyan Actually it should be a while loop instead of recursive call. A function shouldn't call itself just to "go back to the beginning". Recursion should be used for recursive algorithms. – Mark Tolonen Oct 22 '22 at 01:02
  • @AndrewRyan, agreed. – user18798042 Oct 22 '22 at 01:39
  • Welcome to Stack Overflow. Please read [ask] and note well that this is **not a discussion forum**, nor a debugging service. Before posting, please try to create a [mre], and focus the question the the part that actually causes the problem. Ask about **one** specific problem; avoid showing other functionality because that creates possible distractions (other problems). – Karl Knechtel Oct 22 '22 at 05:09

1 Answers1

0

I fixed the indentation of your code and ended the loop in the correct way. I think the problem was that the break command was used on the second loop without ending the first loop which was stuck on "True". I fixed it by rewriting the second loop so that the continuation input is looped infinitely until it recognizes the answer inside the possible_responses set. Once it does, it breaks the second loop.

Next, it checks if play_again is not 'y'. If so, it ends the entire function. Else, it's gonna loop again. Hope this helps! :D

import random

def RPS():
    while True:
        user_action = input("Please enter rock, paper or scissor : ")
        possible_actions = ["rock" , "paper" , "scissor"]
        computer_action = random.choice(possible_actions)

        if user_action == computer_action:
            print(f"Your choice is same as your computer . . . It's a tie!!!")
    
        elif user_action == "rock":
            if computer_action == "scissor":
                print(f"You Won!!! your choice was rock and the computer choice was scissors")
            else:
                print("Paper covers rock! You lose.")

        elif user_action == "paper":
            if computer_action == "rock":
                print(f"You won!!! your choice was rock and the computer choice was rock")
            else:
                print("Scissors cuts paper! You lose.")

        elif user_action == "scissor":
            if computer_action == "paper":
                print(f"You won!!! your choice was scissor and the computer choice was paper")
            else:
                print("Rock smashes scissor. You lose!!!")
  
        possible_responses = {'y', 'n'}
        while True:
            play_again = input("Play again? (y/n): ")
            if play_again.lower() in possible_responses:
                break
            else:
                print('Unrecognized response. Please try again.')
        if play_again.lower()!='y':
            print(f"Alright. good bye")
            return False
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
flowwy
  • 1
  • 1
  • Thanks bro for recorrecting my code but when i copy n paste it in the vscode and when i ran the file it says the same thing. "press any key to continue" – mahdi ehdaei Oct 23 '22 at 16:29
  • I would refer to google 'Avoiding "Press any key to continue" when running console application from vscode' i'm pretty sure it has to do with something about debugging. never encountered that problem yet. sorry for not being that helpful haha – flowwy Oct 29 '22 at 23:43