Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- #imports the random library
- colours = ["R", "G", "B", "Y"]
- a = random.choice(colours)
- b = random.choice(colours)
- c = random.choice(colours)
- d = random.choice(colours)
- print(a,b,c,d)
- #this makes it so we can do opition2. eache letter variable represnts a letter from the list colours and since it is done seperatly we can get repeating letters.
- while True:
- #we have a while loop here so that the user can keep doing unlimited guesses until they win
- correct_answer = 0
- wrong_spot = 0
- print("Please guess the 4 letter code using the letters RGBY")
- guess = input()
- guess1 = guess[0]
- guess2 = guess[1]
- guess3 = guess[2]
- guess4 = guess[3]
- #the user will be able to type the 4 letter code in one line and then the computer will individualize it.
- toCheckGuess = ""
- toCheckAnswer = ""
- if guess1 == a:
- correct_answer +=1
- #if the guess is correct then add it to corrects
- else:
- toCheckGuess += guess1
- toCheckAnswer += a
- #these variables hold the incorrect values to check later to see if they are just in the wrong spot
- if guess2 == b:
- correct_answer +=1
- else:
- toCheckGuess += guess2
- toCheckAnswer += b
- if guess3 == c:
- correct_answer +=1
- else:
- toCheckGuess += guess3
- toCheckAnswer += c
- if guess4 == d:
- correct_answer +=1
- else:
- toCheckGuess += guess4
- toCheckAnswer += d
- if correct_answer == 4:
- #if all 4 are correct then the code was guessed correctly and the loop is broken
- break
- #get a variable to check each letter
- for i in range(0, len(toCheckAnswer)):
- #while checking each one in toCheckAnswer, use j to check each letter in toCheckGuess
- for j in range(0, len(toCheckGuess)):
- #if the pair matches up then its in the wrong spot and can be added to wrong_spot
- if toCheckGuess[j] == toCheckAnswer[i]:
- wrong_spot = wrong_spot + 1
- #Change the characters that matched up in each string so it isnt checked again
- toCheckAnswer = toCheckAnswer[:i] + 'X' + toCheckAnswer[i+1:]
- toCheckGuess = toCheckGuess[:j] + 'O' + toCheckGuess[j+1:]
- print(correct_answer, "correct")
- print(wrong_spot, "correct letter in the wrong spot")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement