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


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

Very basic question. How do I start again?

Started bySeymore4Head <Seymore4Head@Hotmail.invalid>
First post2014-08-21 21:37 -0400
Last post2014-08-21 23:21 -0700
Articles 11 — 8 participants

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


Contents

  Very basic question.  How do I start again?   Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-21 21:37 -0400
    Re: Very basic question.  How do I start again? Ben Finney <ben+python@benfinney.id.au> - 2014-08-22 11:55 +1000
      Re: Very basic question.  How do I start again? Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-21 22:13 -0400
        Re: Very basic question. How do I start again? Chris Angelico <rosuav@gmail.com> - 2014-08-22 12:24 +1000
    Re: Very basic question. How do I start again? Chris Angelico <rosuav@gmail.com> - 2014-08-22 11:58 +1000
      Re: Very basic question. How do I start again? Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-21 22:18 -0400
        Re: Very basic question. How do I start again? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-08-21 23:17 -0400
    Re: Very basic question.  How do I start again?   Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-22 12:58 +1000
    Re: Very basic question.  How do I start again? Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-22 04:26 +0000
    Re: Very basic question.  How do I start again?   Tim Roberts <timr@probo.com> - 2014-08-21 22:56 -0700
      Re: Very basic question. How do I start again? Igor Korot <ikorot01@gmail.com> - 2014-08-21 23:21 -0700

#76763 — Very basic question. How do I start again?

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-08-21 21:37 -0400
SubjectVery basic question. How do I start again?
Message-ID<1e5dv9l3aik5bb1jn4vil731rdl7q6sl3l@4ax.com>
I want to give the computer 100 tries to guess a random number between
1 and 100 picked by the computer.

For the moment I am always using 37 as the random pick.  I want to
change the pick to pick=random.randrange(1,100).  The program works as
expected until the computer gets a correct guess.  I don't know what I
should be doing to restart the program when pick=guess.

It is supposed to let the computer pick a number between 1 and 100 and
then let the computer guess the answer.  If the computer picks a low
number the next guess is supposed to be limited to higher numbers than
the guess.  If the computer picks a high number, the next guess is
supposed to be limited to lower numbers than the first guess.

The program fails when guess=pick

import random
count = 1      #Start the counter at 1
low=1           # the low range of 1 to 10
high=100      #The high range of 1 to 100
pick = 37      # Will change to pick=random.randrange(1,100)
guess = 0     #Guess is the computer's guess at pick
print ("Time to play a guessing game.")
print ("")


while count < 100:
    guess = random.randrange(low,high)
    print (pick, guess)
    if guess == pick:
        print ("correct")

#"What I need is something here that says start over"

    elif guess < pick:
        low=guess+1
        print ("Too low")
    elif guess > pick:
        high=guess-1
        print ("Too high")
    count = count +1

(I can see where adding a 25 then 10 increment later would speed up
the guessing)

[toc] | [next] | [standalone]


#76764

FromBen Finney <ben+python@benfinney.id.au>
Date2014-08-22 11:55 +1000
Message-ID<mailman.13268.1408672574.18130.python-list@python.org>
In reply to#76763
Seymore4Head <Seymore4Head@Hotmail.invalid> writes:

> The program works as expected until the computer gets a correct guess.
> I don't know what I should be doing to restart the program when
> pick=guess.

There isn't a “restart the program” code we can give. But I think you
need only something rather simpler:

> while count < 100:
>     guess = random.randrange(low,high)
>     print (pick, guess)
>     if guess == pick:
>         print ("correct")
>
> #"What I need is something here that says start over"

You can end the current loop with the ‘break’ statement. See the docs
<URL:https://docs.python.org/3/reference/simple_stmts.html#the-break-statement>
to see exactly what its semantics are, and try using that in your code.

Feel free to ask further questions when you've tried that, if it's still
not clear.

-- 
 \         “Religious faith is the one species of human ignorance that |
  `\     will not admit of even the *possibility* of correction.” —Sam |
_o__)                                 Harris, _The End of Faith_, 2004 |
Ben Finney

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


#76766

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-08-21 22:13 -0400
Message-ID<td9dv91bdncnpa46psag7o7lf06iftbrnq@4ax.com>
In reply to#76764
On Fri, 22 Aug 2014 11:55:58 +1000, Ben Finney
<ben+python@benfinney.id.au> wrote:

>Seymore4Head <Seymore4Head@Hotmail.invalid> writes:
>
>> The program works as expected until the computer gets a correct guess.
>> I don't know what I should be doing to restart the program when
>> pick=guess.
>
>There isn't a “restart the program” code we can give. But I think you
>need only something rather simpler:
>
>> while count < 100:
>>     guess = random.randrange(low,high)
>>     print (pick, guess)
>>     if guess == pick:
>>         print ("correct")
>>
>> #"What I need is something here that says start over"
>
>You can end the current loop with the ‘break’ statement. See the docs
><URL:https://docs.python.org/3/reference/simple_stmts.html#the-break-statement>
>to see exactly what its semantics are, and try using that in your code.
>
>Feel free to ask further questions when you've tried that, if it's still
>not clear.

Thanks for the tip, but my trial and error failed.

I tried putting    break_stmt ::=  "break"      at the point where I
want to start over....:) ,but since there is no "start ove"r command,
I was happy to end the program.

I get "invalid syntax so I tried    break_stmt

Traceback (most recent call last):
  File "C:/Documents and Settings/Administrator/Desktop/Python
3.4/Functions/random.py", line 11, in <module>
    guess = random.randrange(low,high)
  File "C:\Python34\lib\random.py", line 196, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" %
(istart, istop, width))
ValueError: empty range for randrange() (37,37, 0)

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


#76768 — Re: Very basic question. How do I start again?

FromChris Angelico <rosuav@gmail.com>
Date2014-08-22 12:24 +1000
SubjectRe: Very basic question. How do I start again?
Message-ID<mailman.13270.1408674258.18130.python-list@python.org>
In reply to#76766
On Fri, Aug 22, 2014 at 12:13 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> I tried putting    break_stmt ::=  "break"      at the point where I
> want to start over....:) ,but since there is no "start ove"r command,
> I was happy to end the program.
>
> I get "invalid syntax so I tried    break_stmt

Ah, that's part of the language of syntax. The page Ben linked you to
is the details of the grammar. This page from the tutorial might make
it a little clearer:

https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

ChrisA

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


#76765 — Re: Very basic question. How do I start again?

FromChris Angelico <rosuav@gmail.com>
Date2014-08-22 11:58 +1000
SubjectRe: Very basic question. How do I start again?
Message-ID<mailman.13269.1408672684.18130.python-list@python.org>
In reply to#76763
On Fri, Aug 22, 2014 at 11:37 AM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> I want to give the computer 100 tries to guess a random number between
> 1 and 100 picked by the computer.
>

Suggestion: Be up-front about this being a homework assignment. Most
of us can tell anyway, and it's more honest that way :)

So, since this is homework, I'm not going to give you the answer. What
I'll do is point you in the direction you need to go.

The most important problem isn't in your code, it's actually here:

> The program fails when guess=pick

In Python, a program doesn't simply fail. It might do the wrong thing
(in which case you should tell us both what it does and what you
expect it to do), or it might terminate with an exception traceback.
Those tracebacks are incredibly useful; when you're asking for help
with a failing program, pasting the entire traceback, including the
full error message, is extremely helpful.

In this case, though, what I'm seeing is that the program will
errantly keep looping when it gets it right. (Not infinitely as you
have the "count < 100" check, but it still loops more than it should.)
So what you need to do is tell it to stop looping when it gets the
right guess. Do you know how to do that?

ChrisA

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


#76767 — Re: Very basic question. How do I start again?

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-08-21 22:18 -0400
SubjectRe: Very basic question. How do I start again?
Message-ID<6q9dv95mpcjrmu7uahltpq6mu2nt7is2en@4ax.com>
In reply to#76765
On Fri, 22 Aug 2014 11:58:00 +1000, Chris Angelico <rosuav@gmail.com>
wrote:

>On Fri, Aug 22, 2014 at 11:37 AM, Seymore4Head
><Seymore4Head@hotmail.invalid> wrote:
>> I want to give the computer 100 tries to guess a random number between
>> 1 and 100 picked by the computer.
>>
>
>Suggestion: Be up-front about this being a homework assignment. Most
>of us can tell anyway, and it's more honest that way :)
>
>So, since this is homework, I'm not going to give you the answer. What
>I'll do is point you in the direction you need to go.
>
>The most important problem isn't in your code, it's actually here:
>
>> The program fails when guess=pick
>
>In Python, a program doesn't simply fail. It might do the wrong thing
>(in which case you should tell us both what it does and what you
>expect it to do), or it might terminate with an exception traceback.
>Those tracebacks are incredibly useful; when you're asking for help
>with a failing program, pasting the entire traceback, including the
>full error message, is extremely helpful.
>
>In this case, though, what I'm seeing is that the program will
>errantly keep looping when it gets it right. (Not infinitely as you
>have the "count < 100" check, but it still loops more than it should.)
>So what you need to do is tell it to stop looping when it gets the
>right guess. Do you know how to do that?
>
>ChrisA

No homework.  This is just for personal enrichment.
Stopping the program when guess==pick is not really what I want the
program to do, but it that is what I am limited to do, that will have
to do.  I am also unable to get that working at the moment.  More
trial and error with the break statement is what I will be doing next.

Thanks

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


#76771 — Re: Very basic question. How do I start again?

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-08-21 23:17 -0400
SubjectRe: Very basic question. How do I start again?
Message-ID<mailman.13272.1408677473.18130.python-list@python.org>
In reply to#76767
On Thu, 21 Aug 2014 22:18:24 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> declaimed the following:

>
>No homework.  This is just for personal enrichment.
>Stopping the program when guess==pick is not really what I want the
>program to do, but it that is what I am limited to do, that will have
>to do.  I am also unable to get that working at the moment.  More
>trial and error with the break statement is what I will be doing next.
>

	Hint: the /break/ statement takes you OUT of the guess /while/ loop.

	You apparently want to go back to the top of the program and start
over, no?

	Sounds like you want another loop, wouldn't you say? (And don't forget
to include some way out of that loop in order to end the program)

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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


#76769

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-08-22 12:58 +1000
Message-ID<53f6b1e1$0$30001$c3e8da3$5496439d@news.astraweb.com>
In reply to#76763
Seymore4Head wrote:

> I want to give the computer 100 tries to guess a random number between
> 1 and 100 picked by the computer.
> 
> For the moment I am always using 37 as the random pick.  I want to
> change the pick to pick=random.randrange(1,100).  The program works as
> expected until the computer gets a correct guess.  I don't know what I
> should be doing to restart the program when pick=guess.
> 
> It is supposed to let the computer pick a number between 1 and 100 and
> then let the computer guess the answer.  If the computer picks a low
> number the next guess is supposed to be limited to higher numbers than
> the guess.  If the computer picks a high number, the next guess is
> supposed to be limited to lower numbers than the first guess.
> 
> The program fails when guess=pick
> 
> import random
> count = 1      #Start the counter at 1
> low=1           # the low range of 1 to 10
> high=100      #The high range of 1 to 100
> pick = 37      # Will change to pick=random.randrange(1,100)
> guess = 0     #Guess is the computer's guess at pick
> print ("Time to play a guessing game.")
> print ("")
> 
> 
> while count < 100:
>     guess = random.randrange(low,high)
>     print (pick, guess)
>     if guess == pick:
>         print ("correct")
> 
> #"What I need is something here that says start over"

Start over as in "go back to the beginning".

There is no specific way to jump backwards to a previous line of code in
Python. Python has two ways to loop, `while` and `for`. In this case
`while` is the answer.

The idea is to think about the high-level structure of the game.

# Not python code, this is pseudo-code
while you want to play again:
    play a game
    ask "Do you want to play again?"


Now you can fill in some of the details, using a stub for things which will
be filled in later.

again = True  # Start off wanting to play at least once.
while again:
    print("pretend that we just played a game")  # a stub
    # Use input in Python 3, raw_input in Python 2.
    again = input("Would you like to play again? [y/n] ") == "y"


That will (pretend to) play a game at least once, then give you the choice
to continue or not. If you would rather play a fixed number of games:

for counter in range(10):  # Play 10 games.
    print("pretend that we just played a game")


Now let's stop pretending:


import random
again = True  # Start off wanting to play at least once.
while again:
    # Here we actually play the game.
    count = 1      # Start the counter at 1
    low = 1        # The low range of 1 to 100
    high = 100     # The high range of 1 to 100
    pick = random.randrange(1,100)
    guess = 0      # Guess is the computer's guess at pick
    print("Time to play a guessing game.")
    print()
    while count < 100:
        guess = random.randrange(low,high)
        print(pick, guess)
        if guess == pick:
            print("Correct!")
            # Break out of this loop, which returns us to
            # the outer loop.
            break

        elif guess < pick:
            # And so on...

    # Use input in Python 3, raw_input in Python 2.
    again = input("Would you like to play again? [y/n] ") == "y"



Notice that indentation is important: the size of the indent tells Python
whether you are inside the inner loop, the outer loop, or the top level.
But hopefully you already know that.

As this starts getting bigger and more unwieldy, it's time to learn about
defining your own custom functions. But you'll get to that.


-- 
Steven

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


#76772

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-08-22 04:26 +0000
Message-ID<lt6gp9$tr7$1@dont-email.me>
In reply to#76763
On Thu, 21 Aug 2014 21:37:22 -0400, Seymore4Head wrote:

> I want to give the computer 100 tries to guess a random number between 1
> and 100 picked by the computer.
> 
> For the moment I am always using 37 as the random pick.  I want to
> change the pick to pick=random.randrange(1,100).  The program works as
> expected until the computer gets a correct guess.  I don't know what I
> should be doing to restart the program when pick=guess.
> 
> It is supposed to let the computer pick a number between 1 and 100 and
> then let the computer guess the answer.  If the computer picks a low
> number the next guess is supposed to be limited to higher numbers than
> the guess.  If the computer picks a high number, the next guess is
> supposed to be limited to lower numbers than the first guess.
> 
> The program fails when guess=pick
> 
> import random count = 1      #Start the counter at 1 low=1           #
> the low range of 1 to 10 high=100      #The high range of 1 to 100 pick
> = 37      # Will change to pick=random.randrange(1,100)
> guess = 0     #Guess is the computer's guess at pick print ("Time to
> play a guessing game.")
> print ("")
> 
> 
> while count < 100:
>     guess = random.randrange(low,high)
>     print (pick, guess)
>     if guess == pick:
>         print ("correct")
> 
> #"What I need is something here that says start over"
> 
>     elif guess < pick:
>         low=guess+1 print ("Too low")
>     elif guess > pick:
>         high=guess-1 print ("Too high")
>     count = count +1
> 
> (I can see where adding a 25 then 10 increment later would speed up the
> guessing)

Write the problem out in basic english terms, then translate these to the 
program. The english might look like this (laid out in a pythonic manner):

while I want to play a game:
    choose a number
    guess the answer
    tries = 1
    while guess != choice:
        guess another answer
        tries = tries + 1
    print "it took " + tries + " attempts to guess " + choice

This simplification doesn't take the calculation of ranges into account, 
but that's part of "guess the/another answer".

-- 
Denis McMahon, denismfmcmahon@gmail.com

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


#76774

FromTim Roberts <timr@probo.com>
Date2014-08-21 22:56 -0700
Message-ID<hrmdv9tk3pvoiacnogsho42ae9redictee@4ax.com>
In reply to#76763
Seymore4Head <Seymore4Head@Hotmail.invalid> wrote:
>
>I want to give the computer 100 tries to guess a random number between
>1 and 100 picked by the computer.

If it takes more than 7, you're doing it wrong...
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

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


#76776 — Re: Very basic question. How do I start again?

FromIgor Korot <ikorot01@gmail.com>
Date2014-08-21 23:21 -0700
SubjectRe: Very basic question. How do I start again?
Message-ID<mailman.13274.1408688502.18130.python-list@python.org>
In reply to#76774
Hi,

On Thu, Aug 21, 2014 at 10:56 PM, Tim Roberts <timr@probo.com> wrote:
> Seymore4Head <Seymore4Head@Hotmail.invalid> wrote:
>>
>>I want to give the computer 100 tries to guess a random number between
>>1 and 100 picked by the computer.
>
> If it takes more than 7, you're doing it wrong...

I think he meant:
100 runs of the script...

Thank you.

> --
> Tim Roberts, timr@probo.com
> Providenza & Boekelheide, Inc.
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [standalone]


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


csiph-web