Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67319 > unrolled thread

Re: Help with "Guess the number" script

Started byDennis Lee Bieber <wlfraed@ix.netcom.com>
First post2014-03-01 11:35 -0500
Last post2014-03-03 02:10 -0500
Articles 11 — 7 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Help with "Guess the number" script Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-03-01 11:35 -0500
    Re: Help with "Guess the number" script Susan Aldridge <susanaldridge555@gmail.com> - 2014-03-01 10:03 -0800
    Re: Help with "Guess the number" script Scott W Dunning <swdunning@cox.net> - 2014-03-01 18:11 -0700
      Re: Help with "Guess the number" script Larry Hudson <orgnut@yahoo.com> - 2014-03-01 23:38 -0800
      Re: Help with "Guess the number" script Scott W Dunning <swdunning@cox.net> - 2014-03-02 18:36 -0700
    Re: Help with "Guess the number" script Chris Angelico <rosuav@gmail.com> - 2014-03-02 12:16 +1100
    Re: Help with "Guess the number" script Scott W Dunning <swdunning@cox.net> - 2014-03-02 18:40 -0700
    Re: Help with "Guess the number" script Scott W Dunning <swdunning@cox.net> - 2014-03-02 20:44 -0700
    Re: Help with "Guess the number" script Ben Finney <ben+python@benfinney.id.au> - 2014-03-03 14:52 +1100
    Re: Help with "Guess the number" script Scott W Dunning <swdunning@cox.net> - 2014-03-02 21:04 -0700
    Re: Help with "Guess the number" script Dave Angel <davea@davea.name> - 2014-03-03 02:10 -0500

#67319 — Re: Help with "Guess the number" script

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-03-01 11:35 -0500
SubjectRe: Help with "Guess the number" script
Message-ID<mailman.7516.1393691734.18130.python-list@python.org>
On Fri, 28 Feb 2014 23:46:02 -0700, Scott W Dunning <swdunning@cox.net>
declaimed the following:

>Hello, i am working on a project for learning python and I’m stuck.  The directions are confusing me.  Please keep in mind I’m very ne to this.  The directions are long so I’ll just add the paragraphs I’m confused about and my code if someone could help me out I’d greatly appreciate it!  Also, we haven’t learned loops yet so just
conditional operators and for some reason we can’t use global variables.  
>
	Without loops, one part of your assignment is going to be tedious,
unless the intent is to only allow for one guess per run.

>
>from random import randrange
>randrange(1, 101)

	You generated a random number and then threw it away.
>
>from random import seed
>seed(129)

	Now you are seeding the random number generator but never generate a
new number after it.

>    
>def print_description():
>    print """Welcome to Guess the Number.
>    I have seleted a secret number in the range 1 ... 100.
>    You must guess the number within 10 tries.
>    I will tell you if you ar high or low, and
>    I will tell you if you are hot or cold.\n"""
>   
>def get_guess(guess_number):
>    print "(",guess_number,")""Plese enter a guess:"
>    current_guess = raw_input()
>    return int(guess_number)

	You're returning the counter of how many guesses have been made, and
dropping the player's guess into the trash

>
>def main():
>    print_description()
>    secret = 50

	That's supposed to be the random number, isn't it?

>    current_guess = 1
>    get_guess(1)

	You drop the return value into the trash

>    if current_guess != secret():

	You are /calling/ a function called secret -- which doesn't exist,
since you bound it to the integer value 50; And current_guess doesn't exist
in this function either.

>        print "Congratulations you win!!"


	Read the documentation on how function calls and return values operate.
Hint: you need to do something with the returned value /at the point where
you call the function/.

	And -- as has been mentioned; these mistakes are better served in the
tutor list, as they would be mistakes in /any/ common programming language.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [next] | [standalone]


#67330

FromSusan Aldridge <susanaldridge555@gmail.com>
Date2014-03-01 10:03 -0800
Message-ID<233865a4-cd75-4466-811b-d3cb644c279c@googlegroups.com>
In reply to#67319
Try this

def guess1(upLimit = 100):
    import random
    num = random.randint(1,upLimit)
    count = 0
    gotIt = False
    while (not gotIt):
        print('Guess a number between 1 and', upLimit, ':')
        guess= int(input())
        count += 1
        if guess == num:
              print('Congrats! You win')
              gotIt = True
        elif guess < num:
              print('Go up!')
        else:
              print('Guess less')
    print('You got it in ', count, 'guesses.')

guess1(100)

[toc] | [prev] | [next] | [standalone]


#67399

FromScott W Dunning <swdunning@cox.net>
Date2014-03-01 18:11 -0700
Message-ID<mailman.7555.1393722671.18130.python-list@python.org>
In reply to#67319
On Mar 1, 2014, at 11:03 AM, Susan Aldridge <susanaldridge555@gmail.com> wrote:
> Try this
> 
> def guess1(upLimit = 100):
>    import random
>    num = random.randint(1,upLimit)
>    count = 0
>    gotIt = False
>    while (not gotIt):
>        print('Guess a number between 1 and', upLimit, ':')
>        guess= int(input())
>        count += 1
>        if guess == num:
>              print('Congrats! You win')
>              gotIt = True
>        elif guess < num:
>              print('Go up!')
>        else:
>              print('Guess less')
>    print('You got it in ', count, 'guesses.')
> 
> guess1(100)
Thanks Susan!  The only problem is he wants us to do it without loops because we haven’t learned them yet. I need to use the variables and function names that he’s given as well.  I think I can make sense of what you wrote though so that should help me a bit.  

[toc] | [prev] | [next] | [standalone]


#67414

FromLarry Hudson <orgnut@yahoo.com>
Date2014-03-01 23:38 -0800
Message-ID<1ZidnQpoVoB9Qo_OnZ2dnUVZ_rmdnZ2d@giganews.com>
In reply to#67399
On 03/01/2014 05:11 PM, Scott W Dunning wrote:
>
> On Mar 1, 2014, at 11:03 AM, Susan Aldridge <susanaldridge555@gmail.com> wrote:
>> Try this
>>
>> def guess1(upLimit = 100):
>>     import random
>>     num = random.randint(1,upLimit)
>>     count = 0
>>     gotIt = False
>>     while (not gotIt):
>>         print('Guess a number between 1 and', upLimit, ':')
>>         guess= int(input())
>>         count += 1
>>         if guess == num:
>>               print('Congrats! You win')
>>               gotIt = True
>>         elif guess < num:
>>               print('Go up!')
>>         else:
>>               print('Guess less')
>>     print('You got it in ', count, 'guesses.')
>>
>> guess1(100)
> Thanks Susan!  The only problem is he wants us to do it without loops because we haven’t learned them yet. I need to use the variables and function names that he’s given as well.  I think I can make sense of what you wrote though so that should help me a bit.
>

Another 'problem' is what you failed to mention in your post, but is apparent from the 
instructions that you posted -- this assignment is NOT the complete program, just the beginning 
of one.  Your instructor obviously wants you to work on (and understand) this program fragment 
before continuing with the rest of it.

      -=- Larry -=-

[toc] | [prev] | [next] | [standalone]


#67495

FromScott W Dunning <swdunning@cox.net>
Date2014-03-02 18:36 -0700
Message-ID<mailman.7614.1393810574.18130.python-list@python.org>
In reply to#67399

[Multipart message — attachments visible in raw view] — view raw

On Mar 2, 2014, at 12:38 AM, Larry Hudson <orgnut@yahoo.com> wrote:
> 
> Another 'problem' is what you failed to mention in your post, but is apparent from the instructions that you posted -- this assignment is NOT the complete program, just the beginning of one.  Your instructor obviously wants you to work on (and understand) this program fragment before continuing with the rest of it.
> 
No it is the whole program I just didn’t post his entire instructs because they were like 5 pages.  Ifugured I just post what I was struggling with right now.  I’m hoping once I get past this first par I’ll be good to go.  Hopefully!

Scott

[toc] | [prev] | [next] | [standalone]


#67401

FromChris Angelico <rosuav@gmail.com>
Date2014-03-02 12:16 +1100
Message-ID<mailman.7557.1393722973.18130.python-list@python.org>
In reply to#67319
On Sun, Mar 2, 2014 at 12:11 PM, Scott W Dunning <swdunning@cox.net> wrote:
>>    print('You got it in ', count, 'guesses.')
>>
> Thanks Susan!  The only problem is he wants us to do it without loops because we haven’t learned them yet. I need to use the variables and function names that he’s given as well.  I think I can make sense of what you wrote though so that should help me a bit.
>

Another consideration: Susan's code is written for Python 3, but you
seemed to be using Python 2. You'll find that the code won't even run
on your version of Python.

(If you have the chance, ask if the course writer would consider
updating it to use Python 3. There's getting to be less and less
reason to use Python 2 for any new projects; when you start a brand
new system, you should be using Python 3 unless there's something
holding you on the old version. So it's correspondingly more useful to
learn Py3.)

ChrisA

[toc] | [prev] | [next] | [standalone]


#67496

FromScott W Dunning <swdunning@cox.net>
Date2014-03-02 18:40 -0700
Message-ID<mailman.7615.1393810834.18130.python-list@python.org>
In reply to#67319
On Mar 1, 2014, at 6:16 PM, Chris Angelico <rosuav@gmail.com> wrote:
> 
> Another consideration: Susan's code is written for Python 3, but you
> seemed to be using Python 2. You'll find that the code won't even run
> on your version of Python.
> 
> (If you have the chance, ask if the course writer would consider
> updating it to use Python 3. There's getting to be less and less
> reason to use Python 2 for any new projects; when you start a brand
> new system, you should be using Python 3 unless there's something
> holding you on the old version. So it's correspondingly more useful to
> learn Py3.)
> 
> ChrisA

I completely agree.  However, the instructor is wanting to use Python 2.7.6 because the book he is using for the course goes over 2.7.6.  Hopefully, once I learn more it will not be a huge jump to python 3.  

Scott

[toc] | [prev] | [next] | [standalone]


#67505

FromScott W Dunning <swdunning@cox.net>
Date2014-03-02 20:44 -0700
Message-ID<mailman.7620.1393818297.18130.python-list@python.org>
In reply to#67319

[Multipart message — attachments visible in raw view] — view raw

On Mar 2, 2014, at 6:40 PM, Scott W Dunning <swdunning@cox.net> wrote:

This is what Im having trouble with now.  Here are the directions I’m stuck on and what I have so far, I’ll bold the part that’s dealing with the instructions if anyone could help me figure out where I’m going wrong.  

Thanks!

from random import randrange
randrange(1, 101)
from random import seed
seed(129)
    
def print_description():
    print """Welcome to Guess the Number.
    I have seleted a secret number in the range 1 ... 100.
    You must guess the number within 10 tries.
    I will tell you if you ar high or low, and
    I will tell you if you are hot or cold.\n"""
   
def get_guess(guess_number):
    promt = "(" + str(guess_number) +") Please enter a guess:"
    user_guess = raw_input(promt)
    user_guess = int(user_guess)
    return user_guess

def print_hints(secrets, guess):
    secret_number = secret
    guess = guess
    if guess < 0 or user_guess> 101:
        print "Out of range!"

def main():
    print_description()
    secret = randrange(1,101)
    current_guess = get_guess(1)
    if current_guess != secret:
        print_hints(secret_number, guess)
        current_guess = get_guess(2)
        
    if secret == current_guess:
        print "Congratulations, you win!"
    else:
        print "Please play again"
    print "The secret number was", secret        
            
main()
Just below the body of the get guess function, define a new function named print hints that takes two arguments. The first is a secret num- ber and is kept in a parameter named secret. The second is a guess made by the user and it is held in a parameter named guess.

The user’s guess is supposed to be within the range 1 ... 100. Write a conditional statement that checks if the guess is out of that range, and if it is print ‘out of range’ in the body of the print hints function.

Now we are going to give the user the option to make a second guess. You must add code to the main function immediately after assignment statement you wrote for task 7.

Write a conditional statement to check if the current guess does not match the secret number. If the numbers to not match, in the body of the conditional statement you will do two things.

(a)  call print hints to give the user hints,

(b)  re-assign current guess to the result of calling get guess with an

argument of 2. 

[toc] | [prev] | [next] | [standalone]


#67506

FromBen Finney <ben+python@benfinney.id.au>
Date2014-03-03 14:52 +1100
Message-ID<mailman.7621.1393818745.18130.python-list@python.org>
In reply to#67319
Scott W Dunning <swdunning@cox.net> writes:

> This is what Im having trouble with now.

Once again, Scott, this discussion should be happening at the Tutor
forum. Please don't continue the fragmentation of this discussion; keep
the discusson over at the Tutor forum.

-- 
 \      “I like to fill my bathtub up with water, then turn the shower |
  `\       on and pretend I'm in a submarine that's been hit.” —Steven |
_o__)                                                           Wright |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#67507

FromScott W Dunning <swdunning@cox.net>
Date2014-03-02 21:04 -0700
Message-ID<mailman.7622.1393819466.18130.python-list@python.org>
In reply to#67319
On Mar 2, 2014, at 8:52 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> 
> Once again, Scott, this discussion should be happening at the Tutor
> forum. Please don't continue the fragmentation of this discussion; keep
> the discusson over at the Tutor forum.
Sorry, I was just replying to replies to my post.  I get the posts through my email so it’s hard to distinguish, especially since people are responding under both.  I guess since people are responding it’s not that big a deal.

[toc] | [prev] | [next] | [standalone]


#67516

FromDave Angel <davea@davea.name>
Date2014-03-03 02:10 -0500
Message-ID<mailman.7627.1393830372.18130.python-list@python.org>
In reply to#67319
 Scott W Dunning <swdunning@cox.net> Wrote in message:
> 
Here are the directions I’m stuck on and what I have so far, I’ll bold the part 

That assumes that people can see which parts of your message are
 bold. Rather a poor assumption in a text list like these two
 python forums. You should be posting in text, not html.
 

Show some code and explain what you wish it would do. Then explain
 what it actually does, usually by pasting in an exception
 traceback,  or the console output. 

And respond on the tutor forum,  not here.


-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web