Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import random
  2. #imports the random library
  3. colours = ["R", "G", "B", "Y"]
  4.  
  5. a = random.choice(colours)
  6. b = random.choice(colours)
  7. c = random.choice(colours)
  8. d = random.choice(colours)
  9. print(a,b,c,d)
  10. #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.
  11. while True:
  12. #we have a while loop here so that the user can keep doing unlimited guesses until they win
  13. correct_answer = 0
  14. wrong_spot = 0
  15.  
  16. print("Please guess the 4 letter code using the letters RGBY")
  17.  
  18. guess = input()
  19. guess1 = guess[0]
  20. guess2 = guess[1]
  21. guess3 = guess[2]
  22. guess4 = guess[3]
  23. #the user will be able to type the 4 letter code in one line and then the computer will individualize it.
  24.  
  25. toCheckGuess = ""
  26. toCheckAnswer = ""
  27.  
  28.  
  29. if guess1 == a:
  30. correct_answer +=1
  31. #if the guess is correct then add it to corrects
  32.  
  33. else:
  34. toCheckGuess += guess1
  35. toCheckAnswer += a
  36. #these variables hold the incorrect values to check later to see if they are just in the wrong spot
  37.  
  38. if guess2 == b:
  39. correct_answer +=1
  40.  
  41. else:
  42. toCheckGuess += guess2
  43. toCheckAnswer += b
  44.  
  45. if guess3 == c:
  46. correct_answer +=1
  47.  
  48. else:
  49. toCheckGuess += guess3
  50. toCheckAnswer += c
  51.  
  52. if guess4 == d:
  53. correct_answer +=1
  54.  
  55. else:
  56. toCheckGuess += guess4
  57. toCheckAnswer += d
  58.  
  59. if correct_answer == 4:
  60. #if all 4 are correct then the code was guessed correctly and the loop is broken
  61. break
  62.  
  63.  
  64. #get a variable to check each letter
  65. for i in range(0, len(toCheckAnswer)):
  66. #while checking each one in toCheckAnswer, use j to check each letter in toCheckGuess
  67. for j in range(0, len(toCheckGuess)):
  68. #if the pair matches up then its in the wrong spot and can be added to wrong_spot
  69. if toCheckGuess[j] == toCheckAnswer[i]:
  70. wrong_spot = wrong_spot + 1
  71. #Change the characters that matched up in each string so it isnt checked again
  72. toCheckAnswer = toCheckAnswer[:i] + 'X' + toCheckAnswer[i+1:]
  73. toCheckGuess = toCheckGuess[:j] + 'O' + toCheckGuess[j+1:]
  74.  
  75. print(correct_answer, "correct")
  76. print(wrong_spot, "correct letter in the wrong spot")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement