Ask coding questions

← Back to all posts
Can someone help?
s2110751 (0)

Limit users to only 10 attempts to get the "Secret Code".
-Alter the program to use colors instead of numbers.
Include whether there is a correct value in the correct spot AND if there is a correct value but in the wrong spot - just like the real game of Mastermind is played.

#This line imports random module
import random
#This line initialize trial to Yes
tryagain = "Yes"
#This line checks if user wants to try again
while tryagain == "Yes":
	#This line generates random number
	randnum = str(random.randint(999, 9999))
	#This line initialize trial to 1
	trys = 1
	#This line prompts user for guess
	guess = input("Guess: ")
	#This line initializes counter to 0
	count = 0
	#The following while statement keeps repeatung itself until user matches four
	while not count == 4:
		#The following for loop checks if user matches four
		for i in range(0,4):
			if randnum[i] == guess[i]:
				count = count + 1
		#The following if statement is executed if user does not match four
		if not count == 4:
			print("You match "+str(count))
			guess = input("Guess: ")
			trys = trys + 1
		#The following els statement is executed if user does match four
		else:
			print("Congratulations, you match at "+str(trys))
			trys = 1
			tryagain = input("Start? (Yes/No): ")
Answered by sylashur (54) [earned 5 cycles]
View Answer
Comments
hotnewtop
sylashur (54)

If this is a homework question, please take this down, instead, ask specifically what you are having trouble with, such as using if statements to compare variables or comparing strings.