Here are two pieces of code needed to make one program. It is a code solving game.:
# Bringing random number for a randomly generated code
import random
code=[random.randint(1,9), random.randint(0,9), random.randint(0,9), random.randint(0,9)]
print(code)
guesses= 1
# Setting up variables
while True:
print("You have: " , 13 - guesses, " guesses left")
guess = input("Have a guess! Type 'quit' to quit: ")
# Taking a guess away for every incorrect answer, allowing an input
# Checking the user input
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
# Allowing an input exit of the game
# Checking the user input... AGAIN
elif len(guess) != 4:
print("The code is 4 digits long! Go again.")
print('\n')
continue
# Checking length of guess
elif guess.isdigit()== False:
print("Please type in a NUMBER guess!")
print('\n')
continue
# Allowing the user to carry on
And here is the subroutine I need to incorporate:
from collections import Counter
def main():
answers = [3, 3, 4, 4]
guesses = [3, 5, 6, 4]
answers_counter = Counter(answers)
guesses_counter = Counter(guesses)
diff_counter = answers_counter - guesses_counter
number_of_all_correct_digits = len(answers) - len(diff_counter.values())
number_of_correct_digits_in_correct_place = len(
list(filter(lambda x: x == 0, [a - g for a, g in zip(answers, guesses)])))
number_of_correct_digits_in_wrong_place = number_of_all_correct_digits - number_of_correct_digits_in_correct_place
return number_of_correct_digits_in_correct_place, number_of_correct_digits_in_wrong_place
### r1= correct place
### r2 = correct digits in wrong place
if __name__ == "__main__":
r1, r2 = main()
print(r1)
print(r2)
I need to be able to make "answers" the first four digits of code and "guesses" (will be changed to guesses2) to the first four digits of guess. I then need to be able to call on the subroutine to get r1 and r2 to output later on in the program how do I do that?
What I have tried:
Reassigning variables, making new if statements and the obvious "main()"