+ 4
Python - number analyser
Hello sololearners. I'm practicing python. At the moment, I need to make a program that; 1. Generates 4 digit number 2. Asks user for their guess 3. If one of the number in input is presented in a generated number - the program should say which one. 4. Else, it will say wrong. 5. The process wil repeat until user gets the generated number. At the end, number of attempts will also be printed. My current code: https://code.sololearn.com/cDEQdL4RuP54/?ref=app My current output: https://imgur.com/a/bT29DX9 Any suggestions how can I fix it?
27 Answers
+ 4
In 3. the user will always enter a 4 digit number?
And in the for-loop you want to check if an input character is a digit?
Do I understand it correctly?
What should be the result if the sampled number is 1234 and the user enters 1243? Is the order relevant or just the single digits?
+ 4
The order is only tested once the user has all 4 digits correct?
On each guess, you could collect the correctly guessed numbers in a list when you iterate the input (or filter). This way,
– you can get the number of correct letters via length of list
– Have all correctly guessed numbers for the feedback
And maybe you could use a == num before the for-loop: You need to run the for-loop only in cases where a != num.
+ 2
(1) You can print the random numbers so you can compare with the result.
(2) You can put the code in a while loop to do more guesses - But! In SoloLearns app, you have to input all guesses at same time.
+ 1
Yes. Once 4 digits are correct - the order is tested.
Thanks for guidance. I'll try in the near future.
For some reason, I don't feel like I can do much atm, don't know why.
+ 1
Would 10 be an invalid input anyways? (As you ask for 4 digit input?)
+ 1
# Hi, Lamron !
# You can take a look at this code. Maybe it will give you some ideas.
from random import randint
the_random_numbers = [str(randint(0, 9)) for i in range(4)]
the_guess = input("Enter a number with four digits, from 0000 to 9999: ")
print(f"{the_guess[:4]}\n")
guess_lst = list(the_guess[:4])
guess_copy = guess_lst.copy()
tmp_res = []
for k in the_random_numbers:
if k in guess_copy:
tmp_res.append(k)
guess_copy.remove(k)
if guess_lst == the_random_numbers:
print("Congratulations!\b\t-> All digits are correct and in order.")
elif tmp_res:
print("Following of your entered numbers is correct."
" They may, or may not be in correct order:")
res = ""
for k in guess_lst:
if k in tmp_res:
res += k
tmp_res.remove(k)
else:
res += '-'
print('\t->', res)
else:
print("Sorry! None of the entered numbers you guessed were correct.")
+ 1
Hi, Lamron !
Look in my code how I got the random numbers. The user will try to guess at numbers from 0000, 0001, 0002, …, 9999. It’s 10000 combination, and your program has to be able to generate them. Now you miss the first 1000 numbers 0000 to 0999.
Delete the line: option = [‘0’, ‘1’, … ] You don’t need it. It’s confusing.
Take away: ‘options in a’ in your if statement. You already know that ‘option in a’, because option is an iterated element from a.
for options in a:
if options in a and options in num:
…
It’s not a good idea to use the solution you do (if option in a). Instead, look at my solution. If you have a guess with the same digits multple times, like 3331, and the random value is 3218, it vill let you think that all three 3 is correct, even if only one of the 3 is correct. That is why I first create a copy of the user input, and then remove the the value if the number is in the random value, before I test the next value.
+ 1
Per Bratthammar , thanks, will do tomorrow
+ 1
Hi, Lamron !
I’ve tried to anware your questions below:
1 -> Because, as you already stated, you ‘drop’ some numbers sometimes. That means you miss all numbers between 0000 - 0999, and have to run an extra loop to fill up the ‘missing’ numbers. With the solution I suggest, you can avoid that.
2 -> No, you can use an other variable name if you want, but it is not necessary. That’s because the variable k assigns a new value every time it loops. But if you think it’s confusing, just change the variable name to something else that you like.
3 -> Every object in Python can be tested in a if- or a while- statement. It corresponds either to a True or a False value.
Example: A string is False if it is empty, otherwise True. A number is False if it is 0, otherwise True.
So it tmp_res is an empty string, it is False, otherwise True.
+ 1
Per Bratthammar thanks for explanation!
+ 1
Ava and bros scott Please create an own thread for your question.
0
I changed my code. Now I have this output:
https://imgur.com/a/NkHNGes
Update: it all works, but I need somehow to output which numbers are present
0
Yes, the user will always and only enter a 4 digit number.
In for loop I want to check whether the input from the user contains a number between 0 and 9. If it does, the program will output it, if it doesn't nothing will be outputted.
Order is relevant.
Ideally, I want it to be:
Expected input - is a 4digit number.
Expected output - is a string.
Example:
>>>2453
One number is correct. It is 5. Try another number.
>>>5678
Three numbers are correct. It is 5, 7, and 8. Try another number.
>>>5378
All numbers are correct. Try to get the order now.
>>>8735
This is a generated number indeed. You win.
0
Hi Lamron I like your game idea. Some thoughts: your random digit is between 1000 and 9999. Could it be also 0010?
I would test first if a == num so the loop doesn't run if user is yet right.
In the current code it breaks if one digit from a is and also if not in num. The for loop needs to run over every digit to say if it's in num.
Also if a in num there needs to be the test if index of digit in a is the same as index in num.
0
Lamron how do you make this game run in Sololearn? As it needs all input beforehand it would throw EOF while reading line, no? Is there a way to trick that?
0
Julian Zimpel I run it on the other python complier.
And yes, I've made a mistake, it will start from 0000 and end in 9999.
Since an integer is not an iterable, I need to adapt it somehow to get output needed.
0
Oh right, there won't be the leading zeros if it would be like 0010. It will give 10. Maybe testing with len(num) and then concatenate zeros until it's 4 digits like x*"0" + num.
0
0010 -> 10, if it's an integer.
But because it's not iterable, I changed to string, therefore it will remain 0010
0
yes, 10 would be invalid.
Program generates a 4-digit number.
The user has to guess that 4digit number.
The number user enters has to be 4 digits/characters long — If longer - appropriate text will be outputted to notify and inform user on what to enter.
0
Thanks, I'll go through it