Author Topic: [Python] help for a noob  (Read 2052 times)

0 Members and 1 Guest are viewing this topic.

Offline r33per

  • Serf
  • *
  • Posts: 23
  • Cookies: 3
    • View Profile
[Python] help for a noob
« on: 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:


Code: (Python) [Select]

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. 
« Last Edit: April 21, 2012, 08:57:39 pm by r33per »

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [Python] help for a noob
« Reply #1 on: April 08, 2012, 11:03:43 pm »
It should be:
Code: [Select]
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.   
>>>import this
-----------------------------

Offline r33per

  • Serf
  • *
  • Posts: 23
  • Cookies: 3
    • View Profile
Re: [Python] help for a noob
« Reply #2 on: April 08, 2012, 11:24:24 pm »
+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

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [Python] help for a noob
« Reply #3 on: April 08, 2012, 11:52:50 pm »
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!
>>>import this
-----------------------------

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: [Python] help for a noob
« Reply #4 on: April 09, 2012, 12:07:40 am »
I'd stick with the counter and have 2 variables to hold the high & low which would also be used for random range, ex:

Code: [Select]
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:

Code: [Select]
[H]igher, [L]ower, [C]orrect

Offline r33per

  • Serf
  • *
  • Posts: 23
  • Cookies: 3
    • View Profile
Re: [Python] help for a noob
« Reply #5 on: April 09, 2012, 12:14:38 am »
That could work thinking it would look something like
Code: [Select]
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 :)
« Last Edit: April 09, 2012, 12:16:23 am by r33per »

Offline r33per

  • Serf
  • *
  • Posts: 23
  • Cookies: 3
    • View Profile
Re: [Python] help for a noob
« Reply #6 on: April 09, 2012, 01:10:07 pm »
Got it  ;)  thanks to xzid and techb for the help he finished code is :


Code: (Python) [Select]

# 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.")


« Last Edit: April 21, 2012, 08:53:02 pm by r33per »