Trouble in designing a game of guessing a list of numbers in python
Today I want to design a game to guess a list of number set by me. The player should print all the 5 numbers to win the game. And it is not allowed to print the repeated number. The code is as followed:
def guess():
print "Please input a number smaller than 10, let's see if it is in my plan."
print "You should figure out all the 5 numbers."
n = int(raw_input('>'))
my_list = [1, 3, 5, 7, 9]
your_list =
count = 0
while count < 5:
if n in my_list:
if n not in your_list:
your_list.append(n)
count = count + 1
print "Good job! You have got %d numbers!" % count
n = int(raw_input('>'))
else:
print "You have already typed that. Input again!"
n = int(raw_input('>'))
else:
print "That is not what I want."
n = int(raw_input('>'))
print "Here you can see my plan:", my_list
print "You are so smart to guess out, you win!"
guess()
when I tried to run it, I was very confused to see the result:
Please input a number smaller than 10, let's see if it is in my plan
You should figure out all the 5 numbers
>1
Good job! You have got 1 numbers
>2
That is not what I want
>3
Good job! You have got 2 numbers
>5
Good job! You have got 3 numbers
>7
Good job! You have got 4 numbers
>4
Here you can see my plan: [1, 3, 5, 7, 9]
You are so smart to guess out, you win!
When I type 4, it should print "That is not what I want" instead of indicating “win”. Why did it go wrong?
count == 5
3 Answers
3
As of 24th July 2018, the code in question gives indentation errors. Lines 3, 5, 7 and 8 are indented by 3 spaces rather than 4.
When that is corrected the code runs (in Python 2.7), but the "raw_input()" is in the wrong place. In your code:
while count < 5:
# do some stuff
# If previous guess correct, increment count
n = int(raw_input('>')) # This prompts the user again after the 5th success
The raw_input() should be once only:
while count < 5:
n = int(raw_input('>'))
# If previous guess correct, increment count
As Mathieu wrote, user input should always be validated. In this case, if a non-numeric character is entered, the program crashes out with a ValueError exception. This could be caught easily with the Try: Except: idiom:
guess = raw_input('>')
try:
value = int(guess)
except ValueError:
# Handle the error
Another problem is that if "my_list" has one or more duplicate numbers, it is impossible for the game to complete.
One possible solution would be; rather than building a second list for "your_list", you could simply remove elements from "my_list" until there are no elements left:
def guess():
print "Please input a number smaller than 10, let's see if it is in my plan."
print "You should figure out all the 5 numbers."
my_list = [1, 3, 5, 3, 9]
my_plan = str(my_list)
while len(my_list) > 0:
guess = int(raw_input('>'))
try:
int(guess)
except ValueError:
print "'%s' is not a number." % str(guess)
continue
if guess < 0 or guess > 9:
print "ONE digit 0 to 9 please."
else:
if guess in my_list:
my_list.remove(guess) # Remove ONE instance of 'guess'
# or remove ALL instances of 'guess'
# my_list = [num for num in my_list if num != guess]
print "Good job! You have got %d numbers!" % (5 - len(my_list))
else:
print "That is not what I want."
print "Here you can see my plan:", my_plan
print "You are so smart to guess out, you win!"
guess()
(Python 2.x is legacy, Python 3.x is the present and future of the language)
You just had some logic error in your order of raw_input
.
raw_input
You had one extra one at the very beginning of the code.
And you had a raw_input
at the end of each condition. It's better to just have one raw_input
at the beginning of the while loop, and display a message based on that input.
raw_input
raw_input
With your logic, even after you found all 5 guesses, there was still an input waiting, and no matter what that input would be it would display the win message. So even when you typed 4
at the very end you would get the end message because the count<5
condition was completed but the positioning of the raw_input
meant it would still request an input even though you had won.
4
count<5
raw_input
def guess():
print "Please input a number smaller than 10, let's see if it is in my plan."
print "You should figure out all the 5 numbers."
my_list = [1, 3, 5, 7, 9]
your_list =
count = 0
while count < 5:
n = int(raw_input('>'))
if n in my_list:
if n not in your_list:
your_list.append(n)
count = count + 1
print "Good job! You have got %d numbers!" % count
else:
print "You have already typed that. Input again!"
else:
print "That is not what I want."
print "Here you can see my plan:", my_list
print "You are so smart to guess out, you win!"
guess()
Here is an example output with my fixed code:
Please input a number smaller than 10, let's see if it is in my plan.
You should figure out all the 5 numbers.
>1
Good job! You have got 1 numbers!
>2
That is not what I want.
>3
Good job! You have got 2 numbers!
>5
Good job! You have got 3 numbers!
>7
Good job! You have got 4 numbers!
>4
That is not what I want.
>9
Good job! You have got 5 numbers!
Here you can see my plan: [1, 3, 5, 7, 9]
You are so smart to guess out, you win!
On my side, it was working. Only point not working, I needed to input a 6th element before breaking out of the while loop. I'm just going to provide you with an overview of possible improvement and different implementations.
Moreover, you are using python 2. You should consider moving to python 3 especially if you just started to learn it.
Improvements:
while count < 5:
while sorted(my_list) != sorted(your_list):
Implementation:
def guess_2():
print ("Please input a number smaller than 10, let's see if it is in my plan.")
print ("You should figure out all the 5 numbers.")
# Parameters
my_list = [1, 3, 5, 7, 9]
your_list =
count = 0
while sorted(my_list) != (sorted(your_list)):
# Input:
not_valid = True
while not_valid:
try:
n = int(input('>'))
not_valid = False
except:
print ("Please input a number.")
if n in my_list and not n in your_list:
your_list.append(n)
count = count + 1
print ("Good job! You have got %d numbers!" % count)
elif n in my_list and n in your_list:
print ("You have already typed that. Input again!")
else:
print ("That is not what I want.")
print ("Here you can see my plan:", my_list)
print ("You are so smart to guess out, you win!")
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– FHTMitchell
5 hours ago