0

I am new to python, so if this takes some insane experience, try to dumb it down :P

So I was making an unfair rock paper scissors game and wanted to restart the game based on user input

Output >> Wanna play again? Y/N
Input >> Y

(and then it restarts)

and if I say N, the terminal closes. How do I make that a reality?

If you can, try to include my code in there because I might somehow screw it up

Here's the code:

choice = input("Rock, Paper, or Scissors? ")

if choice == "Rock" or choice == "rock":
    print("I choose Paper! \nI win!")

if choice == "Paper":
    print("I choose Scissors! \nI win!")

if choice == "Scissors":
    print("I choose Rock! \nI win!")

if choice == "rock":
    print("I choose Paper! \nI win!")

if choice == "paper":
    print("I choose Scissors! \nI win!")

if choice == "scissors":
    print("I choose Rock! \nI win!")
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
cgdisgood
  • 3
  • 1

3 Answers3

0

Simply make it a while loop and check whether the answer is yes or no.

while input("Continue?: ") == "y":
    ... rest of code

This way it will continue looping for as long as the input is "y" (yes). Put your other input in the "rest of code" bracket, so it asks again for the user input.

To make your coding experience easier, I suggest you read about while loops and input. The while loop will keep on executing for as long as the condition is met. In the above condition, for as long as the user input is "y". You should run the other "input" inside of your while loop, so it won't start an infinite loop (either way, it'd end on your "n", or any other input, for as long as it isn't "y").

Full code:

while input("Would you like to play? (Y/n): ") == "Y":
    choice = input("Rock, Paper, or Scissors? ")

    if choice == "Rock" or choice == "rock":
        print("I choose Paper! \nI win!")

    if choice == "Paper":
        print("I choose Scissors! \nI win!")

    if choice == "Scissors":
        print("I choose Rock! \nI win!")

    if choice == "rock":
        print("I choose Paper! \nI win!")

    if choice == "paper":
        print("I choose Scissors! \nI win!")

    if choice == "scissors":
        print("I choose Rock! \nI win!")

In comparison to the other solutions, this will also ask on the first try.

Nikolas
  • 49
  • 3
0

Instead of restarting the program, try using a loop. A while loop should do the trick.

For example:

user_wants_to_continue = True
while(user_wants_to_continue):
    choice = input("Rock, Paper, or Scissors? ")
    if choice.lower() == 'rock':
        print("I choose scissors!\nI win!")
    if choice.lower() == 'scissors':
        print("I choose paper!\nI win!")
    if choice.lower() == 'paper':
        print("I choose rock!\nI win!")
    user_continue = input("Would you like to play again? [y/n]? ")
    user_wants_to_continue = (user_continue.lower()[0] == 'y')
0

You have essentially asked for a do-while loop where the contents of the loop will always run at least once before checking a condition on whether to continue or now. How to implement a do-while loop is comprehensively covered here.

Using one of the examples from the linked answer, your code could look something like this:

while True:

    choice = input("Rock, Paper, or Scissors? ")
    
    if choice == "Rock" or choice == "rock":
        print("I choose Paper! \nI win!")
    
    if choice == "Paper":
        print("I choose Scissors! \nI win!")
    
    if choice == "Scissors":
        print("I choose Rock! \nI win!")
    
    if choice == "rock":
        print("I choose Paper! \nI win!")
    
    if choice == "paper":
        print("I choose Scissors! \nI win!")
    
    if choice == "scissors":
        print("I choose Rock! \nI win!")
    
    if input("Continue (Y/N)? ") == 'N':
        break
noslenkwah
  • 1,702
  • 1
  • 17
  • 26