[Tutor] Fwd: Re: Simple guessing game - need help with the math

2014-08-13 Thread Greg Markham
My apologies, this reply didn't make its way to the list.

>
> On Aug 13, 2014 1:25 AM, "diliup gabadamudalige" 
wrote:
> >
> > Wouldn't it be easier to work it this way?
> > for example:
> > number range =100
> > your selection = 82
> >
> > round 1
> > computer guesses 50
> > you say higher
> > then in round 2 the computer makes a guess only between 51 and 100
> > computer guesses 92
> > you say lower
> > round 3 computer guesses between 51 and 91
> > round 4 computer guesses 75
> > you say higher
> > now guess is between 76 and 91
> > etc. till the correct answer is reached
> > you can use the random and range to select a random number between two
numbers
>
> Interesting.  I hadn't thought of that. And, it appears that would
circumvent the issue I'm running into with rounding. I will try that
approach; thank-you.
>
> But my curiosity is still begging me for an answer regarding my original
approach. Is there a way to manage the functionality of be round function
such that it does not strip any data to the right of the decimal point?
>
> Thanks,
>
> Greg
>
> >
> >
> >
> > On Wed, Aug 13, 2014 at 4:55 AM, Greg Markham 
wrote:
> >>
> >> Hello again,
> >>
> >> I'm working on one of the more advanced (at this stage of my learning
process), albeit optional, assignments.  The objective is to write a
program that tries to guess your number from 1 to 100.  After each guess,
you tell it if it's (h)igher, (l)ower or (c)orrect, and eventually it'll
logically zero in on the correct value.  In addition, it should count the
number of tries required to come to the correct guess.
> >>
> >> I've worked through a few hiccups so far, but I've come to a
standstill.  Here follows my code:
> >>
> >> import os
> >>
> >> print ("\nShall we play a game?\n\n")
> >> print ("Think of a number, 1 to 100, and I will attempt to guess what
it is.\n")
> >>
> >> input("Press [Enter] when ready.\n")
> >>
> >> guess = 50
> >> change = 50
> >> answer = ""
> >> tries = 1
> >>
> >> while answer == "h" or "l" or "c":
> >> print ("My guess is: ", guess, "\n")
> >> answer = input("Is it (H)igher? (L)ower? Or am I (C)orrect? ")
> >> answer = answer.lower()
> >> if answer == "h":
> >> guess = round(int(guess + (change/2)))
> >> change = change/2
> >> tries += 1
> >> elif answer == "l":
> >> guess = round(int(guess - (guess/2)))
> >> tries += 1
> >> elif answer == "c":
> >> print ("/n/nYay!  I win.  Shall we play again?\n\n")
> >> os.system('cls' if os.name == 'nt' else 'clear')
> >> else:
> >> print ("Invalid response.  Please try again.\n")
> >>
> >>
> >> Something about the math in the code here isn't quite right.  If you
were to enter "h" (for 'higher') each time, it would first guess 50, then
75, then 87, then 93, then 96, and finally would stop progressing at the
number 97.  I suspect it's a side-effect of the way the round function
works, but I don't know how to get around it.
> >>
> >> Any help would be greatly appreciated.
> >>
> >> Thanks in advance,
> >>
> >> Greg
> >>
> >> ___
> >> Tutor maillist  -  Tutor@python.org
> >> To unsubscribe or change subscription options:
> >> https://mail.python.org/mailman/listinfo/tutor
> >>
> >
> >
> >
> > --
> > Diliup Gabadamudalige
> >
> > http://www.diliupg.com
> > http://soft.diliupg.com/
> >
> >
**
> > This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
> >
**
> >
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise to work on

2014-08-13 Thread Greg Markham
Keith,

This should get you started...

http://learnpythonthehardway.org/book/

http://www.codecademy.com/en/tracks/python

http://docs.python-guide.org/en/latest/intro/learning/

Happy coding!

--Greg


On Tue, Aug 12, 2014 at 1:52 PM, keith papa  wrote:

> Hi, am a newbie to python and I wondering if you guys can give me some
> exercise to work on. I have learned: print function , if function ,
> strings, variables, and raw_input. The more the better
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Simple guessing game - need help with the math

2014-08-13 Thread Greg Markham
Hello again,

I'm working on one of the more advanced (at this stage of my learning
process), albeit optional, assignments.  The objective is to write a
program that tries to guess your number from 1 to 100.  After each guess,
you tell it if it's (h)igher, (l)ower or (c)orrect, and eventually it'll
logically zero in on the correct value.  In addition, it should count the
number of tries required to come to the correct guess.

I've worked through a few hiccups so far, but I've come to a standstill.
 Here follows my code:

import os

print ("\nShall we play a game?\n\n")
print ("Think of a number, 1 to 100, and I will attempt to guess what it
is.\n")

input("Press [Enter] when ready.\n")

guess = 50
change = 50
answer = ""
tries = 1

while answer == "h" or "l" or "c":
print ("My guess is: ", guess, "\n")
answer = input("Is it (H)igher? (L)ower? Or am I (C)orrect? ")
answer = answer.lower()
if answer == "h":
guess = round(int(guess + (change/2)))
change = change/2
tries += 1
elif answer == "l":
guess = round(int(guess - (guess/2)))
tries += 1
elif answer == "c":
print ("/n/nYay!  I win.  Shall we play again?\n\n")
os.system('cls' if os.name == 'nt' else 'clear')
else:
print ("Invalid response.  Please try again.\n")


Something about the math in the code here isn't quite right.  If you were
to enter "h" (for 'higher') each time, it would first guess 50, then 75,
then 87, then 93, then 96, and finally would stop progressing at the number
97.  I suspect it's a side-effect of the way the round function works, but
I don't know how to get around it.

Any help would be greatly appreciated.

Thanks in advance,

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Printing multi-line variables horizontally

2014-08-08 Thread Greg Markham
Hello,

Python novice back again.  :)  I'm making progress in my learning process,
but struggling whenever attempting to creatively go beyond what's required
in the various chapter assignments.  For example, there's a simple random
die roller program that looks like the following:


# Craps Roller
# Demonstrates random number generation

import random

# generate random numbers 1 - 6
die1 = random.randint(1, 6)
die2 = random.randrange(6) + 1

total = die1 + die2

print("You rolled a", die1, "and a", die2, "for a total of", total)

input("\n\nPress the enter key to exit.")


I wanted to make it a little more interesting by using ascii art
representations of the six die.  When printing, however, they do so
vertically and not horizontally.  Here's a snippet of the code:


die_1 = """
.-.
| |
|  o  |
| |
`-'"""

die_2 = """
.-.
|o|
| |
|o|
`-'"""

print(die_1, die_2)


So, how would I get this to display horizontally?

Like so...
.-.   .-.
| |   |o|
|  o  |   | |
| |   |o|
`-'   `-'

Thanks,

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python - print function - invalid syntax

2014-08-05 Thread Greg Markham
On Mon, Aug 4, 2014 at 7:38 PM, Steven D'Aprano  wrote:

> On Mon, Aug 04, 2014 at 04:44:46PM -0700, Greg Markham wrote:
>
> >  Ok, when I try this from the Shell window, it works.  When executing the
> > full program from command line, it does not.  Python versions from both
> > shell and command line are 3.4.1 (confirmed via command: "python -V").
>
> I'm a little confused, because I consider "the shell" and "command line"
> to more or less be synonymous. Perhaps if you explain step-by-step what
> you do. Here's my guess of what you mean by "command line":
>
> Click on Start Menu.
> Choose "Run" command.
> Enter "cmd.exe"
> Enter "python I:\Programming\Scripts\Learning\chapter02\game_over2.py"
>
> Am I close?
>

For cmd line, yes that's basically it.  When I say "shell", I'm referring
to the Python IDLE GUI.


>
> > Full error msg output when run from cmd line reads:
> >
> >   File "I:\Programming\Scripts\Learning\chapter02\game_over2.py", line 14
> > print("Here", end=" ")
> >  ^
> > SyntaxError: invalid syntax
>
> That definitely looks like a Python 2 error, but it should work in any
> version of Python 3. This is a most perplexing error, I can only imagine
> that you have both Python 2 and 3 installed and somehow, for some
> unknown reason, you're sometimes getting one and sometimes getting the
> other.
>

That's correct, I did have two versions installed.  I first installed ver
2.7, then later installed ver 3.4 when I began going through the lessons in
the aforementioned book.  Although I'd taken steps to ensure the latest
version was used, I was apparently not thorough enough as when I inserted
some code in the program that revealed the version being used, it stated
2.7.4.  So, I ended up uninstalling both versions, then reinstalling ver
3.4 which reset the file associations and resolved the issue.

Hopefully any further issues I encounter aren't due to misconfigurations
and the like.

Thanks for your help, Steven.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python - print function - invalid syntax

2014-08-05 Thread Greg Markham
On Mon, Aug 4, 2014 at 5:13 PM, Alan Gauld 
wrote:

> On 05/08/14 00:21, Greg Markham wrote:
>
>  but I'm running into a syntax error
>>
>
> As others have said you are getting the expected error when running Python
> v3 code under Python v2.
>
> How exactly are you running the program? It looks like somehow you
> are picking up Python v2 when you run the script even though your python
> -V is showing v3.
>
> BTW. Which OS are you using? It shouldn't affect the cause but
> it helps with the debugging!
>
>
>  For clarity, the full program reads as follows:
>>
>>
>> # Game Over - Version 2
>> # Demonstrates the use of quotes in strings
>> # Python Programming for the Absolute Beginner, 3rd ed - Ch 2
>> # Greg Markham - Aug 04, 2014
>>
>> print("Program 'Game Over' 2.0")
>>
>> print("Same", "message", "as before")
>>
>> print("Just",
>>"a bit",
>>"bigger")
>>
>
> Does all of the above print out correctly? Can you send
> us a cut n paste of the exact output of the program?
>
>
>
>> print("Here", end=" ")
>> print("it is...")
>>
>> print(
>>  """
>>   _   ___   ___  ___   _
>>  /  ___| /   | /   |/   | |  ___|
>>  | |/ /| |/ /|   /| | | |__
>>  | |  _/ ___ |   / / |__/ | | |  __|
>>  | |_| |  / /  | |  / /   | | | |___
>>  \_/ /_/   |_| /_/|_| |_|
>>
>>   _   _ _   _   _
>>  /  _  \ | |   / / |  ___| |  _  \
>>  | | | | | |  / /  | |__   | |_| |
>>  | | | | | | / /   |  __|  |  _  /
>>  | |_| | | |/ /| |___  | | \ \
>>  \_/ |___/ |_| |_|  \_\
>>
>>  """
>>   )
>>
>> input("\n\nPress the enter key to exit.")
>>
>
> finally you could try putting the following at the very top of your
> program:
>
> import sys
> print(sys.version)
>
> That will definitively prove that you are actually
> running v3 on the file...


Aaaah, brilliant!  You were right!

I:\Programming\Scripts\Learning\chapter02>game_over2.py
2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]

I installed ver 2.7.8 initially then some time later installed ver 3.4.1
upon starting the above mentioned book which uses that version.  I thought
I had handled the problem by renaming the executables to python27.exe and
pythonw27.exe as well as modifying the path statement to point to where the
latest version was installed, but that appears not to have been enough.  To
avoid further confusion, I'm uninstalling both versions and reinstalling
the latest now so as to reset the file associations which have become a
little out of sorts from all my tampering.

Thanks for helping me get to the heart of it!

--Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: New to Python - print function - invalid syntax

2014-08-04 Thread Greg Markham
On Mon, Aug 4, 2014 at 1:38 PM, Danny Yoo  wrote:

>
> > > but I'm running into a syntax error when running one of the unmodified
> > > programs.  On line 14, which reads:
> > >
> > > print("Here", end=" ")
> > >
> > > I'm receiving a syntax error which points to the end parameter.
>
> I'd like to also support Joel's suggestion to provide detailed output of
> the error message.  It will help.
>
Here is the full context of the error message:

 File "I:\Programming\Scripts\Learning\Chapter02\game_over2.py", line 14
print("Here", end=" ") #Modified from: print("Here", end=" ")
 ^
SyntaxError: invalid syntax


> > > difference between the version of Python I'm using (3.4.1) and that
> which
> > > was in use at the time the book was written (3.1.x) that is
> responsible for
> > > this error.
>
> Just to double check: how are you confirming what version of Python you're
> using?
>
>From command line, C:\> python -V


> Best of wishes!
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python - print function - invalid syntax

2014-08-04 Thread Greg Markham
On Mon, Aug 4, 2014 at 11:52 AM, Joel Goldstick 
wrote:

> On Mon, Aug 4, 2014 at 1:28 PM, Greg Markham 
> wrote:
> > Hello,
> >
> > I'm extremely new to Python having only just started learning this week.
> > I'm slowly plodding through a book, Python Programming for the Absolute
> > Beginner, 3rd ed by Michael Dawson.
> >
> > Code is provided for all the scripts found throughout the book (found
> here),
> > but I'm running into a syntax error when running one of the unmodified
> > programs.  On line 14, which reads:
> >
> > print("Here", end=" ")
> >
> > I'm receiving a syntax error which points to the end parameter.  In
> order to
> > confirm this, I modified the code to read:
> >
> > print("Here ")
> >
> > ...which runs without incident.  My best guess is that there's a minor
> > difference between the version of Python I'm using (3.4.1) and that which
> > was in use at the time the book was written (3.1.x) that is responsible
> for
> > this error.
>
> Welcome to the group.  I think you made a typo.  Your code should run.
> I just tried this:
> >>> print("hi", end=" ")
> hi >>>
>

 Ok, when I try this from the Shell window, it works.  When executing the
full program from command line, it does not.  Python versions from both
shell and command line are 3.4.1 (confirmed via command: "python -V").

Full error msg output when run from cmd line reads:

  File "I:\Programming\Scripts\Learning\chapter02\game_over2.py", line 14
print("Here", end=" ")
 ^
SyntaxError: invalid syntax

A couple of things that will make it easier to help:
>
> 1. send mail as plain text.  Rich text causes some readers problems
>

Thanks for the tip.  I'll keep this in mind for future queries & replies.


> 2. copy and paste the code and complete traceback.  retyping things
> make it more likely that what is in your code isn't exactly what you
> typed
>

Apologies, but what is a traceback?  Is this a debugging feature found in
the shell?  (I looked, but didn't see it)

Thanks,

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python - print function - invalid syntax

2014-08-04 Thread Greg Markham
On Mon, Aug 4, 2014 at 12:37 PM, Alex Kleider  wrote:

> On 2014-08-04 10:28, Greg Markham wrote:
>
>> Hello,
>>
>> I'm extremely new to Python having only just started learning this week.
>> I'm slowly plodding through a book, Python Programming for the Absolute
>> Beginner, 3rd ed
>> <http://www.amazon.com/Python-Programming-Absolute-Beginner-
>> Edition/dp/1435455002/ref=sr_1_5?ie=UTF8&qid=1407049249&sr=
>> 8-5&keywords=learning+python>
>>
>> by Michael Dawson.
>>
>> Code is provided for all the scripts found throughout the book (found here
>> <http://www.delmarlearning.com/companions/content/
>> 1435455002/downloads/py3e_source.zip>),
>>
>> but I'm running into a syntax error when running one of the unmodified
>> programs.  On line 14, which reads:
>>
>> *print("Here", end=" ")*
>>
> try
>
> print("Here", end="")
>
> (no space between the quotes)
> The end parameter defaults to newline.
> If you change it to the empty string, there'll be no output of a newline
> character.
>

As I understand it, newline is inferred unless the "end" parameter is
used.  This program is designed merely to illustrate the flexibility of the
print function.  As you'll see below, the output from the line in which I
was receiving an error is intended to read as, "Here it is..."  For
clarity, the full program reads as follows:


# Game Over - Version 2
# Demonstrates the use of quotes in strings
# Python Programming for the Absolute Beginner, 3rd ed - Ch 2
# Greg Markham - Aug 04, 2014

print("Program 'Game Over' 2.0")

print("Same", "message", "as before")

print("Just",
  "a bit",
  "bigger")

print("Here", end=" ")
print("it is...")

print(
"""
 _   ___   ___  ___   _
/  ___| /   | /   |/   | |  ___|
| |/ /| |/ /|   /| | | |__
| |  _/ ___ |   / / |__/ | | |  __|
| |_| |  / /  | |  / /   | | | |___
\_/ /_/   |_| /_/|_| |_|

 _   _ _   _   _
/  _  \ | |   / / |  ___| |  _  \
| | | | | |  / /  | |__   | |_| |
| | | | | | / /   |  __|  |  _  /
| |_| | | |/ /| |___  | | \ \
\_/ |___/ |_| |_|  \_\

"""
 )

input("\n\nPress the enter key to exit.")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] New to Python - print function - invalid syntax

2014-08-04 Thread Greg Markham
Hello,

I'm extremely new to Python having only just started learning this week.
I'm slowly plodding through a book, Python Programming for the Absolute
Beginner, 3rd ed

by Michael Dawson.

Code is provided for all the scripts found throughout the book (found here
),
but I'm running into a syntax error when running one of the unmodified
programs.  On line 14, which reads:

*print("Here", end=" ")*

I'm receiving a *syntax error* which points to the *end *parameter.  In
order to confirm this, I modified the code to read:

*print("Here ")*

...which runs without incident.  My best guess is that there's a minor
difference between the version of Python I'm using (3.4.1) and that which
was in use at the time the book was written (3.1.x) that is responsible for
this error.

Thanks and my apologies for the "greenness" of this question.

Sincerely,

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor