-4

I'm trying to develop a program wherein at the finish of the game the user will input "Yes" to make the game restart, while if the user inputed "Not" the game will end. For my tries, I can't seem to figure out how to make the program work. I'm quite unsure if a double while True is possible. Also, it seems like when I enter an integer the game suddenly doesn't work but when I input an invalidoutpit the message "Error, the inputed value is invalid, try again" seems to work fine. In need of help, Thank You!!

import random
A1=random.randint(0,9)
A2=random.randint(0,9)
A3=random.randint(0,9)
A4=random.randint(0,9)


P1="O"
P2="O"
P3="O"
P4="O"


while True:
    while True:
            try:
                P1=="O" or P2=="O" or P3=="O" or P4=="O"

            print("Here is your Clue :) :", P1,P2,P3,P4)


            guess=int(input("\nTry and Guess the Numbers :). "))


        except ValueError:
            print("Error, the inputed value is invalid, try again")

            continue


        else:

            guess1=int(guess[0])
            guess2=int(guess[1])
            guess3=int(guess[2])
            guess4=int(guess[3])



        if guess1==A1:
            P1="X"
        else:
            P1="O"

        if guess2==A2:
            P2="X"
        else:
            P2="O"

        if guess3==A3:
            P3="X"
        else:
            P3="O"

        if guess4==A4:
            P4="X"
        else:
            P4="O"

else:
        print("Well Done! You Won MASTERMIND! :D")

answer=input("Would you like to play again? (Yes or No) ")

if answer==Yes:
        print ('Yay')
        continue
else:
        print ('Goodbye!')
        break
Bear
  • 11
  • 2
  • I'd suggest you edit your post to concentrate on the small area of the code you're having problems with, rather than pasting the whole program. – rwp Mar 31 '18 at 05:20
  • @rwp I disagree. Generally, yes, but there are errors that span the length of this code, so having the code in its entirety is the only way to see that. – Kang Mar 31 '18 at 05:36
  • I'd recommend you to create a minimal example (a mock-up) where you put a dummy game instead of the the real game, and try to get the repeat/stop logic in place. – Ott Toomet Mar 31 '18 at 05:51

3 Answers3

1

Wrap your game in a function eg:

import sys

def game():
    #game code goes here#

Then at the end, call the function to restart the game.

if answer=='Yes': # You forgot to add single/double inverted comma's around Yes
    print ('Yay')
    game() # calls function game(), hence restarts the game 
else:
    print ('Goodbye!')
    sys.exit(0) # end game
moonwalker7
  • 1,122
  • 3
  • 11
  • 29
equallyhero
  • 171
  • 1
  • 2
  • 16
  • This is a good launching point. There are other issues within the game code that would have to be addressed. – Kang Mar 31 '18 at 05:46
0

try this

import random
def game():
    A1=random.randint(0,9)
    A2=random.randint(0,9)
    A3=random.randint(0,9)
    A4=random.randint(0,9)

    P1="O"
    P2="O"
    P3="O"
    P4="O"

    gueses=[]
    while len(gueses)<=3:
            try:
                P1=="O" or P2=="O" or P3=="O" or P4=="O"

                print("Here is your Clue :) :", P1,P2,P3,P4)


                guess=int(input("\nTry and Guess the Numbers :). "))
                gueses.append(guess)


            except ValueError:
                print("Error, the inputed value is invalid, try again")

                continue
    guess1=gueses[0]
    guess2=gueses[1]
    guess3=gueses[2]
    guess4=gueses[3]
    if guess1==A1:
            P1="X"
    else:
            P1="O"

    if guess2==A2:
            P2="X"
    else:
            P2="O"

    if guess3==A3:
            P3="X"
    else:
            P3="O"
    if guess4==A4:
            P4="X"
    else:
            P4="O"
    if P1=="x" and P2=="x" and P3=="x" and P4=="x":
        print("you won")
    else:
        print("YOUE LOSE")
    print("TRUE ANSWERS", A1,A2,A3,A4)
    print("YOUR ANSWER", gueses)
game()
answer=input("Would you like to play again? (Yes or No) ")

if answer=="Yes":
        print ('Yay')
        game()
else:
    print ('Goodbye!')
0

The previous answers are good starts, but lacking some other important issues. I would, as the others stated, start by wrapping your game code in a function and having it called recursively. There are other issues in the guess=int(input("\nTry and Guess the Numbers :). ")). This takes one integer as the input, not an array of integers. The simplest solution is to turn this into 4 separate prompts, one for each guess. I would also narrow the scope of your error test. I've included working code, but I would read through it and make sure you understand the logic and call flow.

import random
def game():
  A1=random.randint(0,9)
  A2=random.randint(0,9)
  A3=random.randint(0,9)
  A4=random.randint(0,9)

  P1="O"
  P2="O"
  P3="O"
  P4="O"

  while True:
    if P1=="O" or P2=="O" or P3=="O" or P4=="O":
      print("Here is your Clue :) :")
      print(P1,P2,P3,P4)
      try:
        guess1=int(input("\nGuess 1 :). "))
        guess2=int(input("\nGuess 2 :). "))
        guess3=int(input("\nGuess 3 :). "))
        guess4=int(input("\nGuess 4 :). "))
      except ValueError:
        print("Invalid Input")
        continue

      if guess1==A1:
        P1="X"
      else:
        P1="O"

      if guess2==A2:
        P2="X"
      else:
        P2="O"

      if guess3==A3:
        P3="X"
      else:
        P3="O"

      if guess4==A4:
        P4="X"
      else:
        P4="O"

    else:
      print("Well Done! You Won MASTERMIND! :D")
      break
  answer=input("Would you like to play again? (Yes or No) ")
  if answer=="Yes":
    print('Yay')
    game()
  else:
    print('Goodbye!')
game()
Kang
  • 81
  • 6