I am making a game called Codebreaker. A 4 digit number is generated while the user guesses the number. It should output the amount of correct digits (up to 4) in the correct place and amount of correct digits in the WRONG place (up to 4). There are 12 lives and this is what I have done so far
import random
code=[random.randint(1,9), random.randint(0,9), random.randint(0,9), random.randint(0,9)]
code=[2, 3, 6, 3]
display_answer= ["X","X","X","X"]
guesses= 1
corrPlace= 0
wronPlace= 0
instructions= input("Welcome to Codebreaker! Would you like to see the instructions type 'Yes' or 'No' ")
if (instructions == "Yes") or (instructions == "yes"):
print("I will randomly generate a 4 digit number, try and guess it using your keyboard.")
else:
print("Alrighty")
mode= input("What mode would you like to play on? 'Easy', 'Medium' or 'Hard': ")
mode= mode.lower()
if mode== "easy":
print("Let's get on with it!" )
print('\n')
print("I am thinking of a four digit number, try and guess that number!")
while True:
print("You have: " , 13 - guesses, " guesses left")
guess = input("Have a guess! Type 'quit' to quit: ")
if guess == "quit":
print('\n')
print("You quit! The number I was thinking of was: ")
print(code)
print('\n')
print("xX !YOU LOSE! Xx")
break
elif len(guess) != 4:
print("The code is 4 digits long! Go again.")
print('\n')
continue
elif guess.isdigit()== False:
print("Please type in a NUMBER guess!")
print('\n')
continue
if guesses == 12:
print("You have no guesses left...")
print("The number I was thinking of was: ", code)
print("xX !YOU LOSE! Xx")
break
guesses = guesses+1
guess1= int(guess[0])
guess2= int(guess[1])
guess3= int(guess[2])
guess4= int(guess[3])
if guess1== code[0]:
display_answer[0]= guess1
if guess2== code[1]:
display_answer[1]= guess2
if guess3== code[2]:
display_answer[2]= guess3
if guess4== code[3]:
display_answer[3]= guess4
print("You have guessed this part correctly:")
print(display_answer, '\n')
if display_answer[0] != "X" and display_answer[1] != "X" and display_answer[2] != "X" and display_answer[3] != "X":
print('\n')
print("You won! The number I was thinking of was: ")
print(code)
print('\n')
print("xX !YOU WON! Xx")
break
elif mode== "medium":
###
print('\n')
print("I am thinking of a four digit number, try and guess that number!")
print (code)
while True:
print("You have: " , 13 - guesses, " guesses left")
guess = input("Have a guess! Type 'quit' to quit: ")
###
###
if guess == "quit":
print('\n')
print("You quit! The number I was thinking of was: ")
print(code)
print('\n')
print("xX !YOU LOSE! Xx")
break
###
###
elif len(guess) != 4:
print("The code is 4 digits long! Go again.")
print('\n')
continue
###
###
elif guess.isdigit()== False:
print("Please type in a NUMBER guess!")
print('\n')
continue
###
###
if guesses == 12:
print("You have no guesses left...")
print("The number I was thinking of was: ", code)
print("xX !YOU LOSE! Xx")
break
guesses = guesses+1
###
###
guess1= int(guess[0])
guess2= int(guess[1])
guess3= int(guess[2])
guess4= int(guess[3])
###
####
corrPlace= 0
if guess1==code[0]:
corrPlace= (corrPlace+1)
if guess2==code[1]:
corrPlace= (corrPlace+1)
if guess3== code[2]:
corrPlace= (corrPlace+1)
if guess4== code[3]:
corrPlace= (corrPlace+1)
####
#####
wronPlace= 0
if guess1=! code[0]:
if guess1== code[1] or code[2] or code[3]:
wronPlace= (wronPlace+1)
if guess2=! code[1]:
if guess2== code[0] or code[2] or code[3]:
wronPlace= (wronPlace+1)
if guess3!= code[2]:
if guess3== code[0] or code[1] or code[3]:
wronPlace= (wronPlace+1)
if guess4!= code[3]:
if guess4== code[0] or code[1] or code[2]:
wronPlace= (wronPlace+1)
if corrPlace== 4:
wronPlace= 0
#####
###
print ("You have guessed ", corrPlace, " digits in the correct place.")
print ("But there are ", wronPlace, " correct digits in the wrong place.")
print ('\n')
if corrPlace== 4:
print('\n')
print("You won! The number I was thinking of was: ")
print(code)
print('\n')
print("xX !YOU WON! Xx")
break
###
elif mode== "hard":
print ("Under construction")
The problem is with this:
#####
wronPlace= 0
if guess1=! code[0]:
if guess1== code[1] or code[2] or code[3]:
wronPlace= (wronPlace+1)
if guess2=! code[1]:
if guess2== code[0] or code[2] or code[3]:
wronPlace= (wronPlace+1)
if guess3!= code[2]:
if guess3== code[0] or code[1] or code[3]:
wronPlace= (wronPlace+1)
if guess4!= code[3]:
if guess4== code[0] or code[1] or code[2]:
wronPlace= (wronPlace+1)
if corrPlace== 4:
wronPlace= 0
It should output up to four digits but it mimmicks the corrPlace variable. The plan I've done for this piece of code is this:
Quote:
compare code to guess same number
1 to [0]
2 to [1]
3 to [2]
4 to [3]
NOT switch
if same then 1
if different then 0
1=0
0=1
if [1] matched dont compare (FALSE)
if [0] matched dont compare
if [2] matched dont compare
if [3] matched dont compare
if [1] not matched compare (TRUE)
if [0] not matched compare
if [2] not matched compare
if [3] not matched compare
if compared to [1] gives TRUE stop
if false carry on
if compared to [0] gives TRUE stop
if false carry on
if compared to [2] gives TRUE stop
if false carry on
if compared to [3] gives TRUE stop
if false carry on
TRUE= wronPlace+1
4 = max
Please help me change this piece of code into the plan below! I've struggles a while with this
What I have tried:
Before the plan it ried using a while loop, if statemts and everything that led to failure. I managed to come up with the basic plan though using my knowledge of the Boolean field and assessing the situation using 1 and 0 (True and False). I predict the continue keyword should be present