Skip to content
← Back to Community
Hey! I'm a little lost with this can someone help me please?
Profile icon
LeviAckerman8

Computer should generate 4 random numbers from 1 - 9 as the "Secret Code".
User should be prompted for their guess of those four numbers.
After they provide their full guess, the user is told how many are correct.
As long as the user does not get all four correct, they keep getting asked for their guess.
After the user finally gets all of them correct (yes - all four), they are congratulated and then told how many tries it took them.
Technical Requirements:
Use at least one list
Use at least one function with parameters

Answered by RYANTADIPARTHI [earned 5 cycles]
View Answer
Voters
Profile icon
LeviAckerman8
Comments
hotnewtop
Profile icon
RYANTADIPARTHI

what have you tried first?

Profile icon
LeviAckerman8

@RYANTADIPARTHI Nothing. That's the thing, idk how to do it. I guess if I used a list at the beginning to generate four random numbers then I could use a while loop but how would I go about that?

Profile icon
RYANTADIPARTHI

@LeviAckerman8 we aren't supposed to give code, but I'll explain.

  • import random module, and use randint to generate 1, 9
  • make an input
  • check if the user answered by using input == variable.
  • then give the var to see how many tries
  • put all this in a function.

Done!

Profile icon
LeviAckerman8

@RYANTADIPARTHI Ok so this is what I did for the code: from random import shuffle
Numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def eliminate(how_many):
shuffle(Numbers)
for x in range(how_many):
Numbers.pop(-1)
return Numbers
Numbers = eliminate(5)

Profile icon
Coder100

not exactly, you have to generate 4 random numbers, but remember they can repeat @LeviAckerman8

Profile icon
RYANTADIPARTHI

@LeviAckerman8 ok, that's good for now. more?

Profile icon
LeviAckerman8

@Coder100 It worked for me?

Profile icon
Coder100

try this:
https://repl.it/@Coder100/working-repl-bruh-PoliteGoldenrodMemory#main.py

what happens is that you look at the amount correct by setting that to 0, and then you just check if each item matches in the generated array, but then if they do match, you just add correct by 1. Afterward, finally you do it again if the correct wasn't 4