Re: [Tutor] Help with python 2.7

2017-10-30 Thread Mike Miller
This is a mailing list, so the best way to get help is to send a request
with code examples and most importantly what you have already tried. That
being said, there are some smart people on here, much more experienced than
I am on Python so I would say you can get help with any problem you may
have.

I have taken the MIT course you are talking about and it is quite
challenging (perhaps a little more than it should be) and my oldest son at
one point took the Microsoft MVA course on the subject of python and he
said it was very good (surprisingly enough)..he is a Java programmer
primarily and said that for someone with even average skill and ability,
the MS course is actually great and easy to follow on. Good luck, and
welcome.

Mike

On Mon, Oct 30, 2017, 7:24 PM Alchemy  wrote:

> Hello,
>
> I am in the midst of a career change from renovations to computers.
> I’m used to working conceptually and understand coding is that.
> I am taking an online opencourseare classfrom MIT “Introduction to
> computer science and programming”.
> The class uses python 2.7, has video lectures and homework assignments
> that are coding exercises.
> Python seems like the root to everything in the computers possabilities
> and I want to internalize it.
> I’m stuck on the homework ,butmore importantly stuck on the concepts
> behind the homework.
>
> Can your orginazation help?
>
> gratefully,
>
> Ian
>
> ___
> 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


Re: [Tutor] Help with python 2.7

2017-10-30 Thread Alan Gauld via Tutor
On 30/10/17 21:02, Alchemy wrote:

> I’m stuck on the homework ,butmore importantly stuck on the concepts behind 
> the homework.
> 
> Can your orginazation help?

Yes, we are happy to help with homework although we won't
usually give you the full answer. We will make suggestions
and are happy to review or comment on code.

To make life easier:

Always post any code in plain text (email messes up the
formatting of html/rtf) And put it inline not as an attachment.

Always post the full text of any error messages.

Mention the OS and Python versions.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Help with python 2.7

2017-10-30 Thread Alchemy
Hello,

I am in the midst of a career change from renovations to computers.
I’m used to working conceptually and understand coding is that.
I am taking an online opencourseare classfrom MIT “Introduction to computer 
science and programming”.
The class uses python 2.7, has video lectures and homework assignments that are 
coding exercises.
Python seems like the root to everything in the computers possabilities and I 
want to internalize it.
I’m stuck on the homework ,butmore importantly stuck on the concepts behind the 
homework.

Can your orginazation help?

gratefully,

Ian 

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


Re: [Tutor] Help With Python Tasks

2017-09-05 Thread Alan Gauld via Tutor
On 05/09/17 19:25, Ruth Hardy wrote:

> I was wondering if you can help me please with these computer science tasks

We can help, but we won;t do them for you.

> First Task
> 
> Write code do the following things involving strings.  Wherever possible, 
> you should try to incorporate functions/procedures, 
> 
> Print a list of 20 random numbers one after the other vertically on the 
> screen.

There are (at least) two ways to do this. One very easy but laborious,
the other much shorter and quicker.
Do you know what they are? Can you print 20 numbers (forget random for
now) on the screen?

> Half way through, use a user input request to pause printing until 
> input is received.

Do you know how to read user input?

> Ask a user to enter two numbers, one after the other.  

This is slightly ambiguous, but I'd assume they mean
you should ask for input twice.
Do you know how to store user input into a variable?
Do you know how to convert user input to a number?

> Your code should validate that you receive numbers and 
> prompt the user appropriately.  

Do you know how to validate/check that the received
input is a number? (there are several ways to do this,
I'm not sure how they expect you to tackle it, it depends
on what you've been taught so far)


For the:
> first number, use try-except to ensure you have a floating point number; 

OK, That answers my question. You can use try/except
to check the number is valid...

> place a comment in you code about how this works
> second number, use .isnumeric() to check you have an integer; 

OK, They tell you what to do here.
Personally I don;t like the isnumeric() technique but its
what they want you to do so do it...

> Use a forced type conversion to turn both numbers into floats, >divided one 
> by the other and print the answer to 2 d.p.

Do you know how to print a number so it shows two dp?

> You code should keep on asking for numbers until two have been received.

OK, Lets just get it working for a single pair of numbers first.
Done that? Good, so now wrap it in a loop. What kind of loop will
work here?

> Ask the user to input a true/false value and convert th> input to a Boolean 
> (hint: I don’t think you can do a forced
> type conversion for this…)

The hint is correct.
How would you do it without a type conversion?

> Second Task 

Let's leave this task to another email.

See if you can answer the questions above.
If you can you should be able to join the bits together to get
the task completed. If not come back to us with specific
issues sand we'll explain a bit more.

Show us your code plus any error messages you get.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Help With Python Tasks

2017-09-05 Thread Ruth Hardy
Dear Sir or Madam 

I was wondering if you can help me please with these computer science tasks as 
I don’t know where to start:

First Task

Write code do the following things involving strings.  Wherever possible, you 
should try to incorporate functions/procedures, although they are not 
appropriate for all tasks.

Print a list of 20 random numbers one after the other vertically on the screen. 
 Half way through, use a user input request to pause printing until input is 
received.

Ask a user to enter two numbers, one after the other.  Your code should 
validate that you receive numbers and prompt the user appropriately.  For the:

first number, use try-except to ensure you have a floating point number; place 
a comment in you code about how this works
second number, use .isnumeric() to check you have an integer; place a comment 
in your code to explain why this only works for integers
Use a forced type conversion to turn both numbers into floats, divided one by 
the other and print the answer to 2 d.p.  You code should keep on asking for 
numbers until two have been received.

Ask the user to input a true/false value and convert the input to a Boolean 
(hint: I don’t think you can do a forced type conversion for this…)



Second Task 

Write a menu system based on functions where the main program loop is only 2 
lines long

Write a simple calculator program based around a menu loop.  You should be able 
to enter ‘number 1’ and ‘number 2’ which will be displayed on the screen every 
time the menu is redisplayed’; there should also be a clear screen function 
used to avoid the screen getting too cluttered.

The calculator menu should allow the user to:

enter values for ‘number 1’ and ‘number 2’
add ‘number 1’ and ‘number 2’
subtract ‘number 2’ from ‘number 1’
multiply ‘number 1’ and ‘number 2’
divide ‘number 1’ by ‘number 2’
exit cleanly 
You program must display an answer that the user can read after each 
calculation.


I would appreciate your help so much as I need to have a finished copy  of this 
code by tomorrow late morning !

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


Re: [Tutor] Help with Python Queue

2016-11-26 Thread Joaquin Alzola

>I have below code but it is giving this error:
>AttributeError: Queue instance has no attribute 'taskdone'

>import threading
>from Queue import Queue

>def worker(q):
>while True:
>  dataset = q.get()
>  print("q is taken out")
>  q.taskdone()

Per documentation it is task_done (I suppose you are using python 3+)

https://docs.python.org/3.5/library/queue.html


This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Python Queue

2016-11-26 Thread Anish Tambe
> AttributeError: Queue instance has no attribute 'taskdone'

Method name is task_done (not taskdone)

https://docs.python.org/2/library/queue.html#Queue.Queue.task_done
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Python Queue

2016-11-26 Thread Alan Gauld via Tutor
On 26/11/16 09:07, anish singh wrote:

> I have below code but it is giving this error:
> AttributeError: Queue instance has no attribute 'taskdone'

Please post the full error not just a summary.

Also please post the actual code...

> import threading
> from Queue import Queue

I get an import error here with no module named Queue.
Which Queue module are you using? Or should it be
spelled queue?

> def worker(q):
> while True:
>   dataset = q.get()
>   print("q is taken out")
>   q.taskdone()

Assuming you meant the standard queue module then
that should be

q.task_done()

When you get an attribute error it's worth trying
dir() on the offending object:

>>> from queue import Queue as Q
>>> dir(Q)
, task_done,
>>> help(Q.task_done)
...

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Help with Python Queue

2016-11-26 Thread anish singh
I was just writing to read a file using popen and wanted to use queue along
with it.
I have below code but it is giving this error:
AttributeError: Queue instance has no attribute 'taskdone'

import threading
from Queue import Queue

def worker(q):
while True:
  dataset = q.get()
  print("q is taken out")
  q.taskdone()

def create_data(q):
print("Inside create data")
output = [1, 2, 3]
for i in output:
  print("Inside", i)
  print("queue is put")
  q.put(i)

if __name__ == '__main__':
q = Queue()
t = threading.Thread(target=worker, args=(q,))
t.daemon = True
t.start()
create_data(q)
q.join()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python

2016-04-19 Thread Alan Gauld
On 19/04/16 10:13, Tim Golden wrote:

> In any recent version of Windows (ie Vista & later) the most common way 
> to find a program is to press the "Start" button or the "Windows" key 
> and just start typing its name

Interesting, I've been using Windows 10 since it came out and didn't
know about that trick. I also haven't noticed it any of the PC mags I've
read recently.

> The same for the getting a command prompt up: press "Start/Windows" and 
> type "Command". 

I usually hit Windows->R and type cmd.
So that's less useful for me.

But thanks for the tip.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Help with python

2016-04-19 Thread Tim Golden

On 19/04/2016 10:03, Alan Gauld wrote:

However, for now, you probably want to use IDLE which should
come with Python. (It is sometimes called Python GUI on
Windows too.)

You should find it under Python in your All Programs view.


In any recent version of Windows (ie Vista & later) the most common way 
to find a program is to press the "Start" button or the "Windows" key 
and just start typing its name -- ie rather than actually navigating 
through a cascade of menus. (I think on other systems this is called a 
"Finder" or "Launcher" or something).


In this particular case, pressing "Start/Windows" and typing "IDLE" 
gives -- in my case -- several options, including 3.4 64-bit etc. 
Hopefully, for the OP, there will be just one.


The same for the getting a command prompt up: press "Start/Windows" and 
type "Command". Obviously it will depend on what's installed but for me 
the "Command Prompt" icon is the top of the resulting search list.


Hope that helps people who are not used to (recent) Windows and are 
trying to advise novice users.


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


Re: [Tutor] Help with python

2016-04-19 Thread Alan Gauld
On 19/04/16 02:20, Bereke via Tutor wrote:
> Hello there:
> 
>   i am facing some problem with my python programming language which is 
> already installed in My laptop lenovo, 8.1 OS.

I'm assuming that means Windows 8.1?

> The existing language says "python 27 ". when i click on it, 
> it turns to "python or pythonw".

I don't know what you mean when you say "click on it".
Where are you clicking? The desktop? A menu? Windows explorer?
And where does it say python or pythonw?

> I can write on its dark surface, but it does not have tools like file, play, 
> edit,...

It sounds like you are opening the Python interpreter in
a command console. It will be helpful for you to learn how
to use that console. There are several tutorials on the web
if you search for "Windows command line".

However, for now, you probably want to use IDLE which should
come with Python. (It is sometimes called Python GUI on
Windows too.)

You should find it under Python in your All Programs view.

Alternatively you can use Windows Explorer to search for
the idle.bat file. Once you find it you can create a
shortcut to your desktop or your start menu.
3
> what i need is, to be able to program on it, 

You can program using notepad and the console that you have
but IDLE will be a better experience.

On my Windows 10 box IDLE lives in this folder:

C:\Python34\Lib\idlelib

It should be similar for Windows 8.1

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Help with python

2016-04-19 Thread Bereke via Tutor
Hello there:

  i am facing some problem with my python programming language which is already 
installed in My laptop lenovo, 8.1 OS. The existing language says "python 27 ". 
when i click on it, it turns to "python or pythonw". I can write on its dark 
surface, but it does not have tools like file, play, edit,...on the tool box. 
And i can not Copy past from it.
  when i down load a new 2.7.11 or 3.51 or any other version, it dawnloads only 
text, but what i need is, to be able to program on it, i.e to write on it and 
copy- paste my assignments.
  Could you give me some help please?

Best regards

Bere


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


Re: [Tutor] Help with Python code

2016-02-29 Thread Alan Gauld
On 29/02/16 17:31, Donald Woolfolk wrote:
> Hello, 
> 
> I am new to Python 3.5 and I am writing code to get data 
> from a session api call and load that data into a csv file
> or a MySQL database.

That's great, we can help with that, but...

> I am able to make the session call and also to connect to 
> the MySQL db, but I am not able to convert the result (JSON) data.

We can't read minds. You need to show us your code.
Show us your input and output data. Tell us where you are stuck.
Include full error messages where appropriate. Tell us your
OS and Python version too.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Help with Python code

2016-02-29 Thread Donald Woolfolk
Hello, 

I am new to Python 3.5 and I am writing code to get data from a session api 
call and load that data into a csv file or a MySQL database. I am able to make 
the session call and also to connect to the MySQL db, but I am not able to 
convert the result (JSON) data. I would like assistance with this if possible.

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


Re: [Tutor] Help with python in MAC OS 10.6

2015-02-27 Thread Cameron Simpson

On 27Feb2015 02:16, Barbara Heliodora G. Rodrigues 
 wrote:

I'd like to ask for help with an issue I have with python. My MAC is with OS 
10-6.8, darwin kernel, 64 bits. For some reason I updated macports and it 
automatically updated python to 2.7 with a 32 bits library, and it is giving me 
lots of trouble. I can't install any new software that uses python, and 
recently I tried to use some plot (matplotlib) routines that were working fine 
before, simply didn't work, the window with the plot didn't open.Is there a way 
to force macports to install a 64bits version of python?


Since I use MacPorts and Alan doesn't I'll chip in...

First up, you're talking about the MacPorts pythn (in /opt/local) not the 
system Python (in /usr).


This page:

 
http://stackoverflow.com/questions/2111283/how-to-build-64-bit-python-on-os-x-10-6-only-64-bit-no-universal-nonsense

seems to offer useful advice on getting MacPorts to install your preferred 
variant on Python.


Next, re this:

I downloaded and installed the 3.4 and 3.1 myself, but still the computer only 
sees the version macports installed.


You need to clarify what this statement means, probably with examples of how 
you are invoking your python programs, and the output of "which python".


Your Mac should (now) have multiple Pythons installed:

 /usr/bin/python
   Supplied with MacOSX, the system Python. Probably 64-bit python. As the 
   cited web page above says, run:


 file /usr/bin/python

   to see what architecture it is. You can do that for any of the other 
   executables too to check how they were built.


 /opt/local/bin/python
   The MacPorts python (python 2 by default). It should like you need to 
   reinstall this with the right "variants" settings to get it to be 64-bit.


 Your personally built Pythons.

Regarding the last, please outline what options you used to build these. It is 
very important to build/install with a suitable --prefix Configure option to 
put them in the right place: not a specific place, but a place _not_ 
conflicting with the OSX or MacPorts pythons, for example 
/usr/local/python-2.7.7 (adjust to suit).


It is normal to add some symlinks in /usr/local/bin for "python", "python2" etc 
pointing at your preferred executables.


Next, _how_ are you installing extra packages like matplotlib etc?

I would advocate using MacPorts to install these if available; that leaves the 
arrangements in the hands of MacPorts and keeps things simple for you.


Otherwise, I would next suggest learning to use virtualenv to make a specific 
python "environment" for you work. This is easier than it sounds. The 
virtualenv command prepares a direct of your choice (eg $HOME/venv-2.7.7, to 
invent a name) which contains "python" and "pip" executables in the "bin" 
subdirectory which are crafted to automatically install and use packages 
entirely within the virtualenv directory, avoiding conflicts with other systems 
like OSX or MacPorts. This is great for experimentation of maintaining special 
setups of your own.


You can base the virtualenv of any of the installed Pythons (OSX, MacPorts, 
whatever), to get the installed libraries for free.


Please respond with further information and we can proceed from there.

Cheers,
Cameron Simpson 

To have no errors
Would be life without meaning
No struggle, no joy
- Haiku Error Messages 
http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python in MAC OS 10.6

2015-02-27 Thread Alan Gauld

On 27/02/15 02:16, Barbara Heliodora G. Rodrigues wrote:

Dear tutor,
I'd like to ask for help with an issue I have with python. My MAC is with OS 
10-6.8,


I'm not a MacOs expert but I know MacOS uses Python in some of its tools 
so do not mess with the standard version, you might break something.



I can't install any new software that uses python,


What the OS uses as Python should not necessarily impact what you use as 
Python. Usually its just a case of changing a link or creating an alias 
or environment variable.


At the worst case you can edit the shebang line of your scripts.


I tried to use some plot (matplotlib) routines that were working fine before,


How are you running these scripts? Do you just double click an icon or 
do you use a commandline? If the latter what do you type?

If the former somebody with a modern Mac will need to advise.


Is there a way to force macports to install a 64bits version of python?


Probably, but I don't know macports.


I downloaded and installed the 3.4 and 3.1 myself, but still the

> computer only sees the version macports installed.

Have you tried calling python3 instead of just python?
That should find your Python 3.X version.


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Help with python in MAC OS 10.6

2015-02-26 Thread Barbara Heliodora G. Rodrigues
Dear tutor,
I'd like to ask for help with an issue I have with python. My MAC is with OS 
10-6.8, darwin kernel, 64 bits. For some reason I updated macports and it 
automatically updated python to 2.7 with a 32 bits library, and it is giving me 
lots of trouble. I can't install any new software that uses python, and 
recently I tried to use some plot (matplotlib) routines that were working fine 
before, simply didn't work, the window with the plot didn't open.Is there a way 
to force macports to install a 64bits version of python?I downloaded and 
installed the 3.4 and 3.1 myself, but still the computer only sees the version 
macports installed.Thank you!Regards,
Bárbara
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Python

2014-05-16 Thread Steven D'Aprano
Hi Glen, and welcome! My responses below.

On Thu, May 15, 2014 at 09:58:07PM -0400, Glen Chan wrote:

> Hello, I am student trying to fugure out why when I enter any number 
> it says error. It's only suppose to do that if it's out the 1-10 
> range. Please help. Thank you.

You've made an mistake in your logic below. You print an error for 
*every* number:

> number = input('Enter a number between 1 and 10: ')
> while number < 1 or number > 10:
>   print 'Please enter a number between 1 and 10'
>   number = input('Enter a number between 1 and 10: ')

This part is okay. You check that if number is out of range, and if it 
is, it prints a message. Check for yourself:

"Suppose I choose 5. Is 5 less than 1? No. Is it larger than 
10? No. So the while loop immediately ends, and I move on."


So at this stage, you have now successfully asked the user for a number 
between 1 and 10. But here you make the mistake:

> number = input('Enter a number between 1 and 10: ')  

Hmmm. You've already asked the user for a number. But then you ignore 
it, and ask for another one. But even that's not really the mistake:

> while number > 1 or number < 10:
>   print 'Error'
>   number = input('Enter a number between 1 and 10: ')  

And this is where you get the logic backwards. This while loop runs 
forever, or until you manually cancel it. Check for yourself:

"Suppose I choose 5. Is 5 larger than 1? Yes. So the while 
loop continues, and 'Error' is printed. Suppose I choose 0.
Is 0 larger than 1? No. Is 0 less than 10? Yes. So again, 
the while loop continues, and 'Error' is printed.

Are there *any* numbers *smaller* than 1 AND *larger* than
10 at the same time? No, of course not. So the while loop 
condition is always true, and the while loop will always 
run no matter what number I choose."

This second while loop has the logic backwards. You should compare this 
one, the faulty one, with the first one, the correct one. Can you see 
the difference?

A couple of other comments about your code:

* In a few places, you use "input", but in other places, you 
  use "raw_input". You should never use "input". It was a 
  mistake, and has been removed from newer versions of Python.

  Instead, you should write int(raw_input("Enter a number...")).

* You check that number is in range like this:

number < 1 or number > 10

  There's an easier way:

1 <= number <= 10

  which is not only less typing, but makes it more obvious what
  you are trying to do. You want number to be in the range 1 
  through 10 inclusive. That makes it harder to screw up the
  logic:

1 >= number >= 10  # Wrong!

  "What do you mean, 1 is BIGGER than number, which is bigger
  than 10? That's impossible!"




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


Re: [Tutor] Help with Python

2014-05-16 Thread Alan Gauld

On 16/05/14 02:58, Glen Chan wrote:

Hello, I am student trying to fugure out why when I enter any number it
says error.


Because that's what you programmed it to do.
Almost. If you enter 1 or 10 you won't get an error.

Look at your logic:


number = input('Enter a number between 1 and 10: ')
while number < 1 or number > 10:
   print 'Please enter a number between 1 and 10'
   number = input('Enter a number between 1 and 10: ')

number = input('Enter a number between 1 and 10: ')
while number > 1 or number < 10:
   print 'Error'
   number = input('Enter a number between 1 and 10: ')


If the first number you enter is say 7.
It skips the first while loop and asks for another number.
If you enter 7 again
It then goes into the second while loop and reports an error.

The only numbers which don't fall into one of the two loops
are 1 and 10.

BTW It's bad practice to use input() in Python v2 becauise it has 
security issues. Instead use raw_input() and convert to an

int() or float() explicitly.


while not (endProgram == 'yes' or endProgram == 'no'):
print 'Please enter a yes or no'


There is a bug here too since you don't provide a way
for the user to enter a new number. It will loop forever.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


[Tutor] Help with Python

2014-05-16 Thread Glen Chan
Hello, I am student trying to fugure out why when I enter any number it says 
error. It's only suppose to do that  if it's out the 1-10 range. Please help. 
Thank you.
 
number = input('Enter a number between 1 and 10: ')
while number < 1 or number > 10:
  print 'Please enter a number between 1 and 10'
  number = input('Enter a number between 1 and 10: ')
  
number = input('Enter a number between 1 and 10: ')  
while number > 1 or number < 10:
  print 'Error'
  number = input('Enter a number between 1 and 10: ')  
endProgram = raw_input('Do you want to end program? (Enter no or yes): ')
while not (endProgram == 'yes' or endProgram == 'no'):
   print 'Please enter a yes or no'
endProgram = raw_input('Do you want to end program? (Enter no to process a new 
set of scores): ')
 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Python

2014-05-12 Thread jitendra gupta
Hi
This will solve your purpose:
Yes we can write in better way also :
--
#The Dice Game
#add libraries needed
import random

#the main function
def main():
print
#initialize variables
playerOne = 'No Name'
playerTwo = 'No Name'
endProgram ="no"

#call to inputNames
playerOne, playerTwo = inputNames(playerOne, playerTwo)
#while loop to run program again
while endProgram == 'no':
#call to rollDice
winnersName = rollDice(playerOne, playerTwo)

#call to displayInfo
print "Winner is ", winnersName
endProgram = raw_input('Do you want to end program? (Enter yes or
no): ')



#this function gets the players names
def inputNames(playerOne, playerTwo):
playerOne = raw_input("Enter Name")
playerTwo = raw_input("Enter Name")

return playerOne, playerTwo
#this function will get the random values
def rollDice(playerOne, playerTwo):
p1number = random.randint(1, 6)
p2number = random.randint(1, 6)

#this function displays the winner

if p1number == p2number:
winnerName = "TIE"
elif p1number > p2number:
winnerName = playerOne
else:
winnerName = playerTwo
return winnerName

if __name__ == "__main__":
# calls main
main()



On Sun, May 11, 2014 at 8:46 AM, Glen Chan  wrote:

> Hello, I am a student trying to figure out Python. I am getting errors
> that I don't know how to fix. What do you do after you get the error
> message and something is highlighted? Does that have to be deleted? Anyway,
> here is what I mean...
>
>
> #>>> The Dice Game
> #add libraries needed
> import random
> #the main function
> def main():
> print
> #initialize variables
> playerOne = 'No Name'
> playerTwo = 'No Name'
>
> #call to inputNames
> playerOne, playerTwo = inputNames(playerOne, playerTwo)
> #while loop to run program again
> while endProgram == 'no':
> #initialize variables
>  winnersName = 'NO NAME'
>  p1number = 0
>  p2number = 0
> #call to rollDice
>  winnerName = rollDice(p1number, p2number, playerOne, playerTwo,
> winnerName)
>
> #call to displayInfo
>  winnerName
> endProgram = raw_input('Do you want to end program? (Enter yes or
> no): ')
>
>
>
> #this function gets the players names
> def inputNames(playerOne, playerTwo):
> playerOne = raw_input("Enter Name")
> playerTwo = raw_input("Enter Name")
>
> return playerOne, playerTwo
> #this function will get the random values
> def rollDice(p1numer, p2numer, playerOne, playerTwo, winnerName):
>  p1number = random.randint(1, 6)
>  p1number = random.randint(1, 6)
>
> #this function displays the winner
>
> if p1number == p2number:
> winnerName = "TIE"
> elif p1number > p2number:
> winnerName = playerOne
> else:
> winnerName = playerTwo
> return winnerName
>
> # calls main
> main()
>
>
> ___
> 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


Re: [Tutor] Help with Python

2014-05-11 Thread Dave Angel

On 05/10/2014 11:16 PM, Glen Chan wrote:

Hello, I am a student trying to figure out Python. I am getting errors that I 
don't know how to fix. What do you do after you get the error message and 
something is highlighted? Does that have to be deleted? Anyway, here is what I 
mean...


def main():
 print
 #initialize variables
 playerOne = 'No Name'
 playerTwo = 'No Name'

 #call to inputNames
 playerOne, playerTwo = inputNames(playerOne, playerTwo)
 #while loop to run program again
 while endProgram == 'no':
 #initialize variables
  winnersName = 'NO NAME'
  p1number = 0
  p2number = 0
 #call to rollDice
  winnerName = rollDice(p1number, p2number, playerOne, playerTwo, 
winnerName)

 #call to displayInfo
  winnerName
 endProgram = raw_input('Do you want to end program? (Enter yes or no): 
')



When posting a question, you should always specify the Python version 
and OS, though it probably doesn't matter here.


As others have said, please paste the exact error message.  This is a 
text mailing list, so any highlighting you may have tried to include is 
lost on most of us.  Please post in a text message, not html, as many 
things can go wrong in the re-interpretation, especially in any source code.


I pasted your code into a text editor, saved the file, and ran it a 
terminal window in Python 2.7 under Linux,


davea@think2:~/temppython$ python glen.py
  File "glen.py", line 25
endProgram = raw_input('Do you want to end program? (Enter yes or 
no): ')

^
IndentationError: unexpected indent
davea@think2:~/temppython$

As you can see the callstack shows the line that has a problem, and 
shows s a specific error message.  The problem is that you indented a 
line improperly.  You only add to the indentation within a function, an 
if/else statement statement block, class, with clause, etc.  Ordinary 
statemdents have to line up with the ones before, not counting comments.


If you line up the endProgram line with the previous winnername line, 
this particular error will go away.  But you have others.


I notice you mix spaces and tabs for indentation, which is dangerously 
confusing.  You should stick to one or the other, and I prefer spaces. 
I configured my text editor (emacs) to turn any tabs into 4 spaces, so 
that I won't get into trouble.


in some places you only indent by one space. That's hard to read and you
can wind up with problems from that.  Better to stick with 4, though 
some people seem to prefer 2.


Next problem is:

davea@think2:~/temppython$ python glen.py
  File "glen.py", line 44
elif p1number > p2number:
^
IndentationError: unindent does not match any outer indentation level

This error is because the elif clause does not line up with the if clause.

But a bigger problem in that portion of code is that you're apparently 
starting a new function, but never define it.  No def line follows the 
comment:  #this function displays the winner


If you add that def xxx():  line, and indent the if, then the elif will 
line up as expected.


After fixing that, the next error is:

Traceback (most recent call last):
  File "glen.py", line 51, in 
main()
  File "glen.py", line 15, in main
while endProgram == 'no':
UnboundLocalError: local variable 'endProgram' referenced before assignment


That comes because you have a while statement that refers to the 
variable endProgram, which has not been yet bound to a value.  You need

endProgram = "no"
before that if statement.

In each of these cases, the error message tells you pretty closely 
what's wrong, and where.  You will need to learn to read the error 
messages, the tracebacks.


Your next problem is one of program logic, and I leave it to you.

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


Re: [Tutor] Help with Python

2014-05-11 Thread Alan Gauld

On 11/05/14 04:16, Glen Chan wrote:

Hello, I am a student trying to figure out Python. I am getting errors
that I don't know how to fix. What do you do after you get the error
message and something is highlighted? Does that have to be deleted?


The error doesn't need to be deleted because it appears in the 
interpreter, not in your code. However,...



#>>> The Dice Game


Are you trying to type the whole program into the interactive Python shell?

If so that's the wrong approach. The >>> prompt is only intended
for entering a few lines to experiment and find out how things work.

You should create a new text file and write the code into that.
You can then run the file in the interpreter.

If you are using an IDE (such as IDLE) that usually means you use
the File->New menu and an editor window will appear. There will
then be a Run menu somewhere (in IDLE its Run->Run Module) to
run the program.

If you are using IDLE take a look at Danny's tutorial here:

http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html

As to your error please copy the en tire error message and
send it to us, otherwise we have to guess what the problem
is...

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Help with Python

2014-05-11 Thread C Smith
Hey Glen, include the error you are getting. It will make answering
your question easier. How are you running this program, in an IDE?

On Sat, May 10, 2014 at 11:16 PM, Glen Chan  wrote:
> Hello, I am a student trying to figure out Python. I am getting errors that
> I don't know how to fix. What do you do after you get the error message and
> something is highlighted? Does that have to be deleted? Anyway, here is what
> I mean...
>
>
> #>>> The Dice Game
> #add libraries needed
> import random
> #the main function
> def main():
> print
> #initialize variables
> playerOne = 'No Name'
> playerTwo = 'No Name'
>
> #call to inputNames
> playerOne, playerTwo = inputNames(playerOne, playerTwo)
> #while loop to run program again
> while endProgram == 'no':
> #initialize variables
>  winnersName = 'NO NAME'
>  p1number = 0
>  p2number = 0
> #call to rollDice
>  winnerName = rollDice(p1number, p2number, playerOne, playerTwo,
> winnerName)
>
> #call to displayInfo
>  winnerName
> endProgram = raw_input('Do you want to end program? (Enter yes or
> no): ')
>
>
>
> #this function gets the players names
> def inputNames(playerOne, playerTwo):
> playerOne = raw_input("Enter Name")
> playerTwo = raw_input("Enter Name")
>
> return playerOne, playerTwo
> #this function will get the random values
> def rollDice(p1numer, p2numer, playerOne, playerTwo, winnerName):
>  p1number = random.randint(1, 6)
>  p1number = random.randint(1, 6)
>
> #this function displays the winner
>
> if p1number == p2number:
> winnerName = "TIE"
> elif p1number > p2number:
> winnerName = playerOne
> else:
> winnerName = playerTwo
> return winnerName
>
> # calls main
> main()
>
>
> ___
> 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] Help with Python

2014-05-11 Thread Glen Chan
Hello, I am a student trying to figure out Python. I am getting errors that I 
don't know how to fix. What do you do after you get the error message and 
something is highlighted? Does that have to be deleted? Anyway, here is what I 
mean...
 
 
#>>> The Dice Game
#add libraries needed
import random
#the main function
def main():
print
#initialize variables
playerOne = 'No Name'
playerTwo = 'No Name'

#call to inputNames
playerOne, playerTwo = inputNames(playerOne, playerTwo)
#while loop to run program again
while endProgram == 'no':
#initialize variables
 winnersName = 'NO NAME'
 p1number = 0
 p2number = 0
#call to rollDice
 winnerName = rollDice(p1number, p2number, playerOne, playerTwo, winnerName)

#call to displayInfo
 winnerName
endProgram = raw_input('Do you want to end program? (Enter yes or no): 
')



#this function gets the players names
def inputNames(playerOne, playerTwo):
playerOne = raw_input("Enter Name")
playerTwo = raw_input("Enter Name")   
 
return playerOne, playerTwo
#this function will get the random values
def rollDice(p1numer, p2numer, playerOne, playerTwo, winnerName):
 p1number = random.randint(1, 6)
 p1number = random.randint(1, 6)
 
#this function displays the winner
 
if p1number == p2number:
winnerName = "TIE"
elif p1number > p2number:
winnerName = playerOne
else:
winnerName = playerTwo
return winnerName

# calls main
main()
 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python

2013-12-02 Thread Steven D'Aprano
On Mon, Dec 02, 2013 at 04:33:27PM -0600, Blake wrote:

> I'm writing a program to calculate totals and change for a menu, and 
> I'm having a few issues.  If you could help me, it would be greatly 
> appreciated.

Would you like us to guess what issues you are having?

Let me look into my crystal ball... I see... a cat walking on your 
keyboard... Do you have a cat? Keep it off your keyboard, and your 
programs will be much better.

No no, don't thank me, it's all part of the service!

*wink*


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


Re: [Tutor] Help with python

2013-12-02 Thread Mark Lawrence

On 02/12/2013 22:33, Blake wrote:

I'm writing a program to calculate totals and change for a menu, and I'm having 
a few issues.  If you could help me, it would be greatly appreciated.



A little more data would help :)  Some code, the OS and Python versions 
and the precise nature of the issues would be a good starting point.  If 
you've not already read it here's a good starting point for how to put 
your question(s) together http://sscce.org/


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


[Tutor] Help with python

2013-12-02 Thread Blake
I'm writing a program to calculate totals and change for a menu, and I'm having 
a few issues.  If you could help me, it would be greatly appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Python in ArcGIS 10.1!

2013-06-23 Thread bob gailer

On 6/17/2013 10:26 AM, Jacobs, Teri (CDC/NIOSH/DSHEFS) (CTR) wrote:


Hi,


Hi - welcome to the tutor list. Be aware that we are a few volunteers.

Your question is one very long line. Please in future ensure it is 
wrapped so we don't have to scroll.


I have wrapped it here.


I have a command line

What is a "command line"?

to spread geoprocessing operations across multiple processes
to speed up performance. However, I do not
know how to import the command line (or at least import it properly)
in Python 2.7.2. Here's the script example given on ArcGIS 10.1 Help:
  
import arcpy


# Use half of the cores on the machine.

arcpy.env.parallelProcessingFactor = "50%"

I tried typing it


What is "it". You show 3 lines of code above. Do you mean all 3 lines?


into the command line


What is "the command line"? Do you mean Command Prompt or Python shell?


but nothing happened.


What did you expect?
Did you hit enter after each line?
Did the cursor move to the next line?
Did you get another prompt?
What did the prompt look like?

I think you get the idea - you need to tell us more, since we did not 
watch you try the above.


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


[Tutor] Help with Python in ArcGIS 10.1!

2013-06-23 Thread Jacobs, Teri (CDC/NIOSH/DSHEFS) (CTR)
Hi,


I have a command line to spread geoprocessing operations across multiple 
processes to speed up performance. However, I do not know how to import the 
command line (or at least import it properly) in Python 2.7.2. Here's the 
script example given on ArcGIS 10.1 Help:



import arcpy

# Use half of the cores on the machine.
arcpy.env.parallelProcessingFactor = "50%"

I tried typing it into the command line but nothing happened. Instructions 
would be much appreciated!!!

Teri A. Jacobs
Geography Fellow
Surveillance Branch/DSHEFS/NIOSH/CDC
513-841-4338
w...@cdc.gov

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


Re: [Tutor] Help with Python Program

2012-02-26 Thread Robert Sjoblom
> import random #import random number generator module
> target = random.randint(1,100) #generates random number between 1 and 100
> guess = float(raw_input('pick a number between 1 and 100'))
> while guess != target:
>    if guess < target: print 'too low'
>    elif guess > target: print 'too high'
>    #otherwise guess == target and game is over
>
> I am having trouble getting it to run for more than one game but am
> trying. Also I started off using the idle shell to write the codes but
> it wont let you run it from there, so I opened a new window and
> retyped it for my first few attempts. I am unsure why you would use
> the idle shell if you would need to open a new window and retype it
> anyway.
[snip]
> so can I always
> do it that way and not use the shell at all?

The shell is for testing out things; it's not really for writing
entire programs in. But as a test-tool it's priceless. Don't know what
random.randint() does? Use the shell to find out. Want to see if you
can convert ints to floats? Use the shell. It's a great way to test
short things like that, because it will give you instant feedback. You
can write longer code in it, but it can be cumbersome. In the end,
it's another tool for your belt, I guess you could say.

> I am also still kind of
> unsure about the loops and was hoping you could explain a little more
> about it or may know of a good online resource?

The while loop you're using tests for one thing: is guess == target.
If it isn't, the contents of the while loop executes. I should note
that while your loop works, it doesn't work as intended: what happens
if you guess too low? Let's see how the program runs:

computer picks a number between 1 and 100 and stores it in variable 'target'
computer then asks user for a number between 1 and 100 and stores it
in variable 'guess'
while loop checks whether guess is the same as target.
If it's not, it checks if the guess is higher than target or lower
than target and then prints the corresponding response.
the while loop checks again if guess == target. It's not, since we
never change the value of guess.
It checks if the guess is higher than target or lower than target...(etc)
the while loop checks again...

In short, you can say that the loop is named because of its behaviour:
it loops until it shouldn't any longer.

I'm sure you can see the problem here. So, while you now can check
whether a variable is equal to another variable, you currently have no
way to change 'guess' for a new try. Can you think of any way to solve
that?

And while this might not be entirely on topic, there's a wonderful
book for complete beginners -- I've used it as well -- that I'd like
to recommend: Python Programming for the Absolute Beginner. In fact,
one of the excercises happen to be this particular game.

--
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Python Program

2012-02-26 Thread Carolina Dianne LaCourse
First off I want to really thank you for all of the help! I am in my
first semester as I started as a non traditional student in January.
Even though I am in an intro class I think that many of my class mates
had more of a c/s foundation in high then I did. It took me a few
tries but I did finally get my program to run using this code :)

#import random target = random.randint(1,100)

import random #import random number generator module
target = random.randint(1,100) #generates random number between 1 and 100
guess = float(raw_input('pick a number between 1 and 100'))
while guess != target:
if guess < target: print 'too low'
elif guess > target: print 'too high'
#otherwise guess == target and game is over

I am having trouble getting it to run for more than one game but am
trying. Also I started off using the idle shell to write the codes but
it wont let you run it from there, so I opened a new window and
retyped it for my first few attempts. I am unsure why you would use
the idle shell if you would need to open a new window and retype it
anyway. Is there an easier way or something that I am not getting?
After doing that several times I just opened a new window and started
from there without using the shell and it worked fine, so can I always
do it that way and not use the shell at all?  I am also still kind of
unsure about the loops and was hoping you could explain a little more
about it or may know of a good online resource? Thanks again for all
the help...Steve really helped by explaining it simply to me and I
really appreciate it! It took me like 10-15 tries but I was so excited
when I finally got it to run!


On 2/24/12, Steven D'Aprano  wrote:
> Carolina Dianne LaCourse wrote:
>
> [...]
>> I understand that I need to ask for raw input from the user and that I
>> need to be able to use the if elif else to tell the user whether their
>> number matches or id too high or to low but am just not sure what to
>> do first. Any advice would be greatly appreciated! I have tried some
>> online tutorials to get the basics but still am having a really hard
>> time. I did work with scratch a bit earlier this semester and got
>> that, but am really struggling with python.
>
> Start by writing down in plain English the steps of how you would play the
> guessing game. This is a called an algorithm, which is something very
> similar
> to a recipe or a set of instructions. You might have something like this:
>
> (1) Think of a number between 1 and 100, and remember it.
> (2) Repeat the following steps until the game is over:
> (3) - Ask the person playing for a number between 1 and 100.
> (4) - If the number is too low, tell them it is too low.
> (5) - If the number is too high, tell them it is too high.
> (6) - If the number is equal to the number you thought of, the game is over.
>
> All that makes up *one* game. Then you need instructions to play multiple
> games:
>
> (a) Play one game, as above.
> (b) Repeat the following steps until done:
> (c) - Ask the player if they want to play again.
> (d) - If they say Yes, play one game, as above.
> (e) - Otherwise, we are done.
> (f) Finally, print how many games were played, how many guesses were needed,
> and the average number of guesses per game.
>
>
> Now, you need to change the English instructions to instructions the
> computer
> can follow, using Python. For example, Step (1) above picks a random number
> and remembers it as the target of the game:
>
> import random
> target = random.randint(1, 100)
>
> Step (2) is a bit harder -- it's a loop. You should have learned about while
> loops and for loops. I expect a while loop is better for this, because you
> can't tell ahead of time how many times you need to go round and round the
> loop.
>
>
> while guess != target:
>  Step (3) ask the user for a number, and call it "guess"
>  if guess < target:
>  print "too low"
>  elif guess > target:
>  print "too high"
>  # otherwise guess == target so the game will be over
>
>
> Notice that this isn't exactly Python code. The most obvious problem is the
> line "Step (3)..." which is plain English. You need to replace that with
> code
> to actually ask the user for a number. (Hint: you will need the raw_input
> function.)
>
> Another problem is that the *first* time you enter the loop, the name
> "guess"
> isn't defined. You need to give it a value to start with, before the loop.
> Any
> value will do, so long as it isn't target. I suggest 0.
>
> Does this help you get started? Go ahead and write some code, and see where
> it
> takes you. Piece by piece, step by step, you should work towards replacing
> each bit of English instruction with some Python code to do that.
>
> You should aim to write code to play *one* game first. Get that right,
> first,
> then adapt it to play multiple games.
>
> Write some code, see how it works (or where is fails to work), and anything
> that is unclear, come back and ask.

Re: [Tutor] Help with Python Program

2012-02-24 Thread Steven D'Aprano

Carolina Dianne LaCourse wrote:

[...]

I understand that I need to ask for raw input from the user and that I
need to be able to use the if elif else to tell the user whether their
number matches or id too high or to low but am just not sure what to
do first. Any advice would be greatly appreciated! I have tried some
online tutorials to get the basics but still am having a really hard
time. I did work with scratch a bit earlier this semester and got
that, but am really struggling with python.


Start by writing down in plain English the steps of how you would play the 
guessing game. This is a called an algorithm, which is something very similar 
to a recipe or a set of instructions. You might have something like this:


(1) Think of a number between 1 and 100, and remember it.
(2) Repeat the following steps until the game is over:
(3) - Ask the person playing for a number between 1 and 100.
(4) - If the number is too low, tell them it is too low.
(5) - If the number is too high, tell them it is too high.
(6) - If the number is equal to the number you thought of, the game is over.

All that makes up *one* game. Then you need instructions to play multiple games:

(a) Play one game, as above.
(b) Repeat the following steps until done:
(c) - Ask the player if they want to play again.
(d) - If they say Yes, play one game, as above.
(e) - Otherwise, we are done.
(f) Finally, print how many games were played, how many guesses were needed, 
and the average number of guesses per game.



Now, you need to change the English instructions to instructions the computer 
can follow, using Python. For example, Step (1) above picks a random number 
and remembers it as the target of the game:


import random
target = random.randint(1, 100)

Step (2) is a bit harder -- it's a loop. You should have learned about while 
loops and for loops. I expect a while loop is better for this, because you 
can't tell ahead of time how many times you need to go round and round the loop.



while guess != target:
Step (3) ask the user for a number, and call it "guess"
if guess < target:
print "too low"
elif guess > target:
print "too high"
# otherwise guess == target so the game will be over


Notice that this isn't exactly Python code. The most obvious problem is the 
line "Step (3)..." which is plain English. You need to replace that with code 
to actually ask the user for a number. (Hint: you will need the raw_input 
function.)


Another problem is that the *first* time you enter the loop, the name "guess" 
isn't defined. You need to give it a value to start with, before the loop. Any 
value will do, so long as it isn't target. I suggest 0.


Does this help you get started? Go ahead and write some code, and see where it 
takes you. Piece by piece, step by step, you should work towards replacing 
each bit of English instruction with some Python code to do that.


You should aim to write code to play *one* game first. Get that right, first, 
then adapt it to play multiple games.


Write some code, see how it works (or where is fails to work), and anything 
that is unclear, come back and ask.




--
Steven

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


Re: [Tutor] Help with Python Program

2012-02-24 Thread bob gailer

On 2/24/2012 10:18 PM, Carolina Dianne LaCourse wrote:

Hi,
I have never programed before and am brand new to python also. I am
trying to create a Hi-Lo game and am not really sure where to start.
These are the instructions that I have. I know that I will need to
import the random number generator and have looked up how to do that.
I understand that I need to ask for raw input from the user and that I
need to be able to use the if elif else to tell the user whether their
number matches or id too high or to low but am just not sure what to
do first. Any advice would be greatly appreciated! I have tried some
online tutorials to get the basics but still am having a really hard
time. I did work with scratch

What is scratch?

a bit earlier this semester and got
that, but am really struggling with python.
We get many requests like this. I always wonder why there is a mismatch 
between the course and the student. A beginner's programming class 
should give you the tools you need to solve the problem. Can you help us 
understand why this is not happening for you?


I assume this is not your fault - that there is something wrong with the 
course and/or the instructor.


As a rule we don't write the program for you; we take a look at what 
you've done and help you over the rough spots. Please write any amount 
of code you can and return with that.


For your second project,

What was the first project? How did that go?

  you will create a Python guessing game. In
this game, the user will be asked to guess a number between 1 and 100.
For each guess, you will output one of three things:
 The user guess is correct -- tell the user he or she won,
asking if he/she wants to play again.
 The user guess is too high -- tell him or her so.
 The user guess is too low -- tell him or her so.
 Your program should be able to run multiple games. That is, after
each game, the user should be asked if they want to play again. If
they type yes, then you pick a new number and play again. If they type
no, then the program should say goodbye and exit. You should keep the
following stats about the player's efforts:
 Number of games played
 Total number of guesses made
 You should output the number of games played as well as the
average number of guess per game. This program will require loops
(nested ones, in fact), an if-elif-else statement, and the use of
Python's random number generator.

Do you know how to create a loop? If not, why not?
Do you know how to test for low or high? If not why not?
If your answers to these questions is no then (IMHO) you are in the 
wrong class.


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


[Tutor] Help with Python Program

2012-02-24 Thread Carolina Dianne LaCourse
Hi,
I have never programed before and am brand new to python also. I am
trying to create a Hi-Lo game and am not really sure where to start.
These are the instructions that I have. I know that I will need to
import the random number generator and have looked up how to do that.
I understand that I need to ask for raw input from the user and that I
need to be able to use the if elif else to tell the user whether their
number matches or id too high or to low but am just not sure what to
do first. Any advice would be greatly appreciated! I have tried some
online tutorials to get the basics but still am having a really hard
time. I did work with scratch a bit earlier this semester and got
that, but am really struggling with python.

For your second project, you will create a Python guessing game. In
this game, the user will be asked to guess a number between 1 and 100.
For each guess, you will output one of three things:
The user guess is correct -- tell the user he or she won,
asking if he/she wants to play again.
The user guess is too high -- tell him or her so.
The user guess is too low -- tell him or her so.
Your program should be able to run multiple games. That is, after
each game, the user should be asked if they want to play again. If
they type yes, then you pick a new number and play again. If they type
no, then the program should say goodbye and exit. You should keep the
following stats about the player's efforts:
Number of games played
Total number of guesses made
You should output the number of games played as well as the
average number of guess per game. This program will require loops
(nested ones, in fact), an if-elif-else statement, and the use of
Python's random number generator.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python-gnupg

2011-03-12 Thread Becky Mcquilling
Thanks, everyone:

Your suggestions worked.  I will make sure to include full information next
time.

Becky

On Sat, Mar 12, 2011 at 12:14 AM, Steven D'Aprano wrote:

> Becky Mcquilling wrote:
>
>> If anyone is familiar with python-gnupg, I am having some difficulty with
>> the syntax.  I've tried the following:
>>
>
> When dealing with third party packages, unless it is an extremely
> well-known package like numpy or nltk, it is usually a good idea to link to
> the project's home page. Do you mean this project?
>
> http://packages.python.org/python-gnupg/
>
> Reading the documentation, I think either of these should work:
>
>
> #1 encrypt data in a file
> input_file = open('c:/test/filename.txt', 'r')
> # notice that you open the file, but do not read from it.
> encrypted_data = gpg.encrypt_file(input_file, 'ladym...@gmail.com',
>output='c:/gpg_test/data.gpg2')
>
>
> #2 encrypt data from a string
> data = open('c:/test/filename.txt', 'r').read()
> encrypted_data = gpg.encrypt_file(data, 'ladym...@gmail.com',
>output='c:/gpg_test/data.gpg2')
>
> It looks like the problem you have is that you are passing the output file
> object, instead of just the file name.
>
>
> If this doesn't solve your problem, please post the exact error message in
> full, including the traceback, with the exact code you are using.
>
>
>
>
> --
> Steven
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python-gnupg

2011-03-12 Thread Steven D'Aprano

Becky Mcquilling wrote:

If anyone is familiar with python-gnupg, I am having some difficulty with
the syntax.  I've tried the following:


When dealing with third party packages, unless it is an extremely 
well-known package like numpy or nltk, it is usually a good idea to link 
to the project's home page. Do you mean this project?


http://packages.python.org/python-gnupg/

Reading the documentation, I think either of these should work:


#1 encrypt data in a file
input_file = open('c:/test/filename.txt', 'r')
# notice that you open the file, but do not read from it.
encrypted_data = gpg.encrypt_file(input_file, 'ladym...@gmail.com',
output='c:/gpg_test/data.gpg2')


#2 encrypt data from a string
data = open('c:/test/filename.txt', 'r').read()
encrypted_data = gpg.encrypt_file(data, 'ladym...@gmail.com',
output='c:/gpg_test/data.gpg2')

It looks like the problem you have is that you are passing the output 
file object, instead of just the file name.



If this doesn't solve your problem, please post the exact error message 
in full, including the traceback, with the exact code you are using.





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


Re: [Tutor] Help with python-gnupg

2011-03-11 Thread David Hutto
On Sat, Mar 12, 2011 at 2:07 AM, David Hutto  wrote:
> As a matter of fact, looking at them with know
*no*

 knowledge of the
> module, it says it's a typeerror, and that it expects string or
> buffer, but gets file. If this is the same error in both instances,
> then it's that output needs to be a string or buffer, so just string
> either the datae variable, or the output variable.
>



-- 
According to theoretical physics, the division of spatial intervals as
the universe evolves gives rise to the fact that in another timeline,
your interdimensional counterpart received helpful advice from me...so
be eternally pleased for them.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python-gnupg

2011-03-11 Thread David Hutto
As a matter of fact, looking at them with know knowledge of the
module, it says it's a typeerror, and that it expects string or
buffer, but gets file. If this is the same error in both instances,
then it's that output needs to be a string or buffer, so just string
either the datae variable, or the output variable.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python-gnupg

2011-03-11 Thread David Hutto
On Sat, Mar 12, 2011 at 1:39 AM, Becky Mcquilling
 wrote:
> If anyone is familiar with python-gnupg, I am having some difficulty with
> the syntax.  I've tried the following:
> f = open('c:/test/filename.txt', 'r')
> datae = gpg.encrypt_file(f.read(), 'ladym...@gmail.com',
> output=open('c:/gpg_test/data.gpg2', 'w'))
>
> or
> file_to_encrypt = open('c:/gpg_test/data.gpg2', 'w')
> datae = gpg(f.read(), 'ladym...@gmail.com', output=file_to_encrypt)
> Either way, I can't get the output written to a file, it gives me an error:
> Traceback (most recent call last):
>   File "", line 1, in 
>     datae = gpg.encrypt_file(f.read(), 'becky...@google.com',
> output=open('c:/test/data.gpg2', 'w'))
>   File "C:\Python27\lib\site-packages\gnupg.py", line 583, in encrypt_file
>     if os.path.exists(output):
>   File "C:\Python27\lib\genericpath.py", line 18, in exists
>     os.stat(path)
> TypeError: coercing to Unicode: need string or buffer, file found

This seems to say it needs a string or buffer, but a file was found.
Which says to me, you need to convert the file that is found to a
string before passing it as a parameter to a function. It might be
that output needs to be a string before it is used, so read the file
and string it. It's just a guess from what I see though.


> Any thoughts?  Would reallly appreciate the help.
> If you aren't familiar with this and know of resources, it would be awesome.
>
> Becky
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
According to theoretical physics, the division of spatial intervals as
the universe evolves gives rise to the fact that in another timeline,
your interdimensional counterpart received helpful advice from me...so
be eternally pleased for them.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python-gnupg

2011-03-11 Thread David Hutto
Show the entire code, and error for both usages. The usages and a
single error message for them both may be enough for someone not to
try them out, and who can help you, if they knew more about the
problem.

Not everyone here will be an expert, but we do read direct code vs
direct error, if we've been paying attention. Plus, we can try the
whole code out, and see what you're trying to do altogether instead of
in a specific portion that may misrepresent the whole of the problem.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with python-gnupg

2011-03-11 Thread Becky Mcquilling
If anyone is familiar with python-gnupg, I am having some difficulty with
the syntax.  I've tried the following:

f = open('c:/test/filename.txt', 'r')
datae = gpg.encrypt_file(f.read(), 'ladym...@gmail.com',
output=open('c:/gpg_test/data.gpg2', 'w'))

or
file_to_encrypt = open('c:/gpg_test/data.gpg2', 'w')
datae = gpg(f.read(), 'ladym...@gmail.com', output=file_to_encrypt)

Either way, I can't get the output written to a file, it gives me an error:

Traceback (most recent call last):
  File "", line 1, in 
datae = gpg.encrypt_file(f.read(), 'becky...@google.com',
output=open('c:/test/data.gpg2', 'w'))
  File "C:\Python27\lib\site-packages\gnupg.py", line 583, in encrypt_file
if os.path.exists(output):
  File "C:\Python27\lib\genericpath.py", line 18, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, file found

Any thoughts?  Would reallly appreciate the help.

If you aren't familiar with this and know of resources, it would be awesome.


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


[Tutor] help with python 3.1

2010-09-28 Thread Ralph Pat Hall
Dear Tutor:

I would like to install a program that was written with python 3.1, the 
computer that I would like to put in on has Win 98SE. I tried Python 3.1.2 MSI 
and 2.61 but got a message that a DLL was not there and would not install. I 
then tried them on a Win 7 computer and got to the 5 files that you had to say 
where they should go. They were register ext.; TCI/TK; documentation; utility 
scripts; test suite---go on the HD, local drive, not at all. My guess is HD ??? 
Do I need all 5 files ?

The writer of the program also said that you needed to download sq-3_6_23_1.zip 
and if you had XP or Vista and you wanted to print you also needed 
pywin.32-214.win32py3.1.exe. Would I need that with 98SE? He also said that any 
python before 3.1 would not be useable.

I don't know anything about python so I need any help possible

thank you 
Ralph
duoble...@live.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor