EvilZone
Programming and Scripting => Scripting Languages => : r33per April 08, 2012, 10:34:47 PM
-
Hi guy's I am learning to script in python and am trying to write a little game where a user thinks of a number between 1 and 100 and the computer tries to guess that number so far i have:
print "Think of a number between 1 and 100."
print "And I will try to guess it."
import random
number = random.randrange(100) + 1
tries = 0
while raw_input != "got it":
print "I think of: ", number
guess = raw_input("higer, lower, got it: ")
if guess == "higher":
number = random.randrange(number, 100)
tries += 1
if guess == "lower":
number = random.randrange(1, number)
tries += 1
print "I got it in ", tries, "go's!"
raw_input("\n\nPress the enter key to exit.")
what i want it to do is remember the the lowest and highest numbers its already guessed and to generate new guesses within that range, also it doesn't exit properly and just re guesses the same number when it gets it right.
I don't want the code but if anyone could give me a nudge in the right direction I would be very grateful.
-
It should be:
while guess != "got it":
#you have, which is wrong
while raw_input != "got it"
The while loop doesn't exit because raw_input is a built-in method and will never equal the string. "guess" is the variable of the raw_input() return.
And to keep track of all numbers guessed, you can append the guessed one's in a list. Once in a list, you can sort it from highest to lowest, or how ever you want and go from there. This will eliminate the need for the "tries" counter because you'll just get the len() of the list.
Also, I like this game idea. I've written ones where the user needs to guess the number, not the other way around. Hell, once you learn enough about python, you can implement some kind of machine learning and optimize the guessing algorithm.
-
+1 kind sir I have already written a game where the computer thinks of a number and the player guesses, but then i thought I'd challenge myself by trying to reverse it, but it seems I've bitten off more then I can chew :) will play around with the code and see if i can get it working not all that confident on len() just yet so will re read that been a good learning exercise so far though
-
Try learning about lists, I think that should be the next step in your endeavors. Just my opinion. Keep learning and don't give up!
-
I'd stick with the counter and have 2 variables to hold the high & low which would also be used for random range, ex:
low = 1;
high = 100;
count = 0;
# in game loop ...
count += 1;
attempt = random_range(low, high);
# print attempt, get answer from user
if(answer == "higher")
low = answer;
else if(answer == "lower")
high = answer;
else if(answer == "correct")
# break from game loop
else
# unknown answer, make user try again
# out of loop ...
# found number "answer" in "count" tries.
works in my head, didn't really test anything. also sorry if gives away too much, just can't explain shit with words.
I would also shorten input so user doesn't have to type out those words on every try, use style:
[H]igher, [L]ower, [C]orrect
-
That could work thinking it would look something like
low = ""
high = ""
if number < low:
low == number
if number > high:
high == number
random.randrange(low, high)
also think the input would work a lot better the way you suggest it's still a work in progress and will need a lot of polishing but think I can finish it now :)
-
Got it ;) thanks to xzid and techb for the help he finished code is :
# Guess your number
# Player thinks of a number between 1 and 100 and the computer tries to guess it
# r33per 9/4/12
print "Think of a number between 1 and 100."
print "And I will try to guess it."
import random
tries = 0
guess = ""
high = 100
low = 1
while guess != "c":
number = random.randrange(low, high)
print "I think of: ", number
guess = raw_input("[h]iger, [l]ower, [c]orrect: ")
if guess == "h":
low = number +1
tries += 1
if guess == "l":
high = number -1
tries += 1
print "I got it in ", tries, "go's!"
raw_input("\n\nPress the enter key to exit.")