Trouble in designing a game of guessing a list of numbers in python

The name of the picture


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?





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





I can not reproduce that output. It will ask for one additoinal input even when count == 5, but it does not stop when count is 4.
– tobias_k
5 hours ago


count == 5





I just copied and paste your code and when I click on 4 I receive "That is not what I want.". So it's working for me on python 2.7
– Carlo 1585
5 hours ago





@Carlo1585 Yeah, I just tried again and received the same result as yours. It was interesting. But when I try to input other numbers, the code still gets wrong. So it is not the fault of version of Python,
– FrancesWu
3 hours ago





@AdamJaamour Yes! Thx:)
– FrancesWu
3 hours ago




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)





Sorry for the indentation :( I sent the code on my iPhone so sth went wrong...Thx for your suggestion! It really helps!
– FrancesWu
2 hours ago



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!





Correction, you'd just fixed his code, with no explaination what-so-ever, I've removed now that you've explained your changes :), much better answer now
– Nick A
5 hours ago







@NickA understandable, I usually add my explanations just after I answer ^^
– Adam Jaamour
5 hours ago





Thx! Your code works very well! And now I understand why I am wrong. Much thx :)
– FrancesWu
3 hours ago



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!")





Thx:)I just start to learn Python and I never consider to code in your way. That really inspires me! Thanks for your suggestion about version of Python. However, I am using the book learn Python the hard way, and the author recommends me to use Python 2 instead of Python 3 to do exercises in his book. So I don' know what to do...
– FrancesWu
3 hours ago





@FrancesWu My opinion is that it isn't smart to start to learn a programming language in a (soon) deprecated version. You could keep going with the same course but using python 3. The error you will get will also teach you about what has been changed between the versions.
– Mathieu
3 hours ago





Get the point. I will try. Thank you!
– FrancesWu
2 hours ago






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.

Popular posts from this blog

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

CRM reporting Extension - SSRS instance is blank

Keycloak server returning user_not_found error when user is already imported with LDAP