-1
all_options = ["rock", "paper", "scissors"]
outcomes = {"rockP": ["paper", "computer"], "rockS": ["scissor", "player"], "paperR": ["rock", "player"], "paperS": ["scissor", "computer"], "scissorsR": ["rock", "computer"], "scissorsP": ["paper", "player"]}
input_loop = True

def play_again():
    again = input("Would you like to play again?(Yes or No): ")
    if again.lower() == "yes":
        continue
    elif again.lower() == "no":
        exit()
    else:
        print("Invalid input. Exiting")
        exit()
while True:
    while input_loop == True:
        choice = input("Please input Rock, Paper, or Scissors: ")
        if choice.lower() in all_options: #Making sure that the input is lowercase so it doesn't error
            choice = choice.lower()
            input_loop = False
            break
        else:
            print("\n That is not a valid input \n")
    computer_choice = random.choice(all_options)
    if choice == computer_choice:
        print("Draw!")
        play_again()
    computer_choice = computer_choice.upper()
    current_outcome = choice + computer_choice
    current_outcome_list = outcomes.get(current_outcome)
    current_outcome_list.insert(0, choice)
    player, winner, outcome = current_outcome_list

    winning_message = print(f"{winner} has won, \nplayer chose: {player}, \ncomputer chose: {computer}.")
    print(winning_message)
    play_again()

How can I use that function with the continue statement? Or any other method to avoid me repeating that chunk of code. I am new to python and I need to add enough words to allow me to post this. Thank you for everybody who is going to help me :)

Error message: C:\desktop 2\files\Python projects\rock paper scissiors\Method2>gamelooped.py File "C:\desktop 2\files\Python projects\rock paper scissiors\Method2\gamelooped.py", line 9 continue ^ SyntaxError: 'continue' not properly in loop

Robby S
  • 11
  • 1
  • 6
  • Hello, could you please include in your question the error message that you get in its entirety? When you post questions online that refer to an error message, please always include the error message. – Steele Farnsworth Dec 15 '20 at 02:50
  • Use quotes or code block and put all your error inside – NanoPish Dec 15 '20 at 09:32
  • Does this answer your question? [syntaxError: 'continue' not properly in loop](https://stackoverflow.com/questions/14312869/syntaxerror-continue-not-properly-in-loop) – Sören Jun 11 '22 at 09:44

1 Answers1

1

It sounds like you may not be familiar with the purpose of the continue keyword. It can only be used in loops. When you reach a continue statement in a for or while loop, none of the remaining statements in the loop are executed, and the loop continues from the beginning of the next iteration (if there is another iteration to be had).

You can't continue in a function unless you're in a loop that's in a function. What is it that you intended for that continue statement to do? It might be that you just need to return.

Steele Farnsworth
  • 863
  • 1
  • 6
  • 15
  • I want the continue statement to start back at the top of the while loop. I want the user to say whether or not they want to continue playing the game and if the answer is yes the game starts back from the beginning. – Robby S Dec 15 '20 at 02:58
  • 1
    @RobbyS A `continue` only makes sense directly inside a loop, not in a function, even if the function is only called from inside a loop. For example, what would happen if you call the function while not being in a loop? – decorator-factory Dec 15 '20 at 03:02
  • @RobbyS Consider returning a boolean from a function, and doing either `break` or `continue` depending on that. – decorator-factory Dec 15 '20 at 03:03
  • This is a constraint of the language: If you're in a function that's being called by a loop, you can't use the `continue` keyword in the function. You can only use `continue` in the loop itself. You'll have to return some value, like `False`, and have an `if` statement in the loop that decides if you should `continue`. – Steele Farnsworth Dec 15 '20 at 03:03
  • Do you recommend any other way I can not repeat that code over and over again? If I do not use the function I will have that same block of code repeated almost 4 times. – Robby S Dec 15 '20 at 03:04
  • 1
    Oh so you're saying I should just store whether or not the user gave yes or no and if it is that then I can exit or break within the loop itself. THANK YOU – Robby S Dec 15 '20 at 03:05