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

2014-08-21 Thread Igor Korot
Hi,

On Thu, Aug 21, 2014 at 10:56 PM, Tim Roberts  wrote:
> Seymore4Head  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, t...@probo.com
> Providenza & Boekelheide, Inc.
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-08-21 Thread Tim Roberts
Seymore4Head  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, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-08-21 Thread Denis McMahon
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, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-08-21 Thread Steven D'Aprano
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

-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-08-21 Thread Seymore4Head
On Fri, 22 Aug 2014 11:58:00 +1000, Chris Angelico 
wrote:

>On Fri, Aug 22, 2014 at 11:37 AM, Seymore4Head
> 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
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-08-21 Thread Chris Angelico
On Fri, Aug 22, 2014 at 12:13 PM, Seymore4Head
 wrote:
> I tried puttingbreak_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 triedbreak_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
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-08-21 Thread Seymore4Head
On Fri, 22 Aug 2014 11:55:58 +1000, Ben Finney
 wrote:

>Seymore4Head  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
>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 puttingbreak_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 triedbreak_stmt

Traceback (most recent call last):
  File "C:/Documents and Settings/Administrator/Desktop/Python
3.4/Functions/random.py", line 11, in 
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)

-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-08-21 Thread Chris Angelico
On Fri, Aug 22, 2014 at 11:37 AM, Seymore4Head
 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
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-08-21 Thread Ben Finney
Seymore4Head  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
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

-- 
https://mail.python.org/mailman/listinfo/python-list


Very basic question. How do I start again?

2014-08-21 Thread Seymore4Head
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)
-- 
https://mail.python.org/mailman/listinfo/python-list