[Tutor] While Loop?

2014-05-14 Thread Sam Ball
I'm attempting to create a program where the user inputs their account number 
(which must be 8 digits) and if what the user enters is not 8 digits in length 
I want python to tell the user this is invalid and then keep asking for the 
account number until a suitable number has been entered.
 I'm guessing it's a sort of while loop but I'm not 100% sure as I'm quite new 
to Python.
 
So far I have...



 
userAccountNumber = eval(input("Please Input Your Account Number:"))
while userAccountNumber > 1 or <=999:
print userAccountNumber ("Invalid Account Number! Account Must Be Eight 
Digits")
 
 
 
When I run this however it gives me an invalid syntax and highlights the = in 
<=999:
 
Would appreciate any help with sorting this out.
 
Thank you.
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] for the error about 'import site' failed; use -v for traceback

2014-05-14 Thread Tao Zhu
Hi everyone,
when I use python, the problem occured. when I used the command "python -v", 
the results are listed as follows. could you tell me what wrong?

$ python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/local/lib/python2.4/site.pyc matches /usr/local/lib/python2.4/site.py
import site # precompiled from /usr/local/lib/python2.4/site.pyc
# /usr/local/lib/python2.4/os.pyc matches /usr/local/lib/python2.4/os.py
import os # precompiled from /usr/local/lib/python2.4/os.pyc
import posix # builtin
# /usr/local/lib/python2.4/posixpath.pyc matches 
/usr/local/lib/python2.4/posixpath.py
import posixpath # precompiled from /usr/local/lib/python2.4/posixpath.pyc
# /usr/local/lib/python2.4/stat.pyc matches /usr/local/lib/python2.4/stat.py
import stat # precompiled from /usr/local/lib/python2.4/stat.pyc
# /usr/local/lib/python2.4/UserDict.pyc matches 
/usr/local/lib/python2.4/UserDict.py
import UserDict # precompiled from /usr/local/lib/python2.4/UserDict.pyc
# /usr/local/lib/python2.4/copy_reg.pyc matches 
/usr/local/lib/python2.4/copy_reg.py
import copy_reg # precompiled from /usr/local/lib/python2.4/copy_reg.pyc
# /usr/local/lib/python2.4/types.pyc matches /usr/local/lib/python2.4/types.py
import types # precompiled from /usr/local/lib/python2.4/types.pyc
# /usr/local/lib/python2.4/warnings.pyc matches 
/usr/local/lib/python2.4/warnings.py
import warnings # precompiled from /usr/local/lib/python2.4/warnings.pyc
# /usr/local/lib/python2.4/linecache.pyc matches 
/usr/local/lib/python2.4/linecache.py
import linecache # precompiled from /usr/local/lib/python2.4/linecache.pyc
import encodings # directory /usr/local/lib/python2.4/encodings
# /usr/local/lib/python2.4/encodings/__init__.pyc matches 
/usr/local/lib/python2.4/encodings/__init__.py
import encodings # precompiled from 
/usr/local/lib/python2.4/encodings/__init__.pyc
# /usr/local/lib/python2.4/codecs.pyc matches /usr/local/lib/python2.4/codecs.py
import codecs # precompiled from /usr/local/lib/python2.4/codecs.pyc
import _codecs # builtin
# /usr/local/lib/python2.4/encodings/aliases.pyc matches 
/usr/local/lib/python2.4/encodings/aliases.py
import encodings.aliases # precompiled from 
/usr/local/lib/python2.4/encodings/aliases.pyc
# /usr/local/lib/python2.4/encodings/utf_8.pyc matches 
/usr/local/lib/python2.4/encodings/utf_8.py
import encodings.utf_8 # precompiled from 
/usr/local/lib/python2.4/encodings/utf_8.pyc
Python 2.4.4 (#3, May 15 2014, 00:22:35)
[GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/usr/local/lib/python2.4/lib-dynload/readline.so", 2);
import readline # dynamically loaded from 
/usr/local/lib/python2.4/lib-dynload/readline.so
>>>
Tao
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loop?

2014-05-14 Thread Alan Gauld

On 14/05/14 10:45, Sam Ball wrote:

I'm attempting to create a program where the user inputs their account
number (which must be 8 digits) and if what the user enters is not 8
digits in length I want python to tell the user this is invalid and then
keep asking for the account number until a suitable number has been entered.
  I'm guessing it's a sort of while loop


You guess correct


userAccountNumber = eval(input("Please Input Your Account Number:"))


But please don;t do this. it is a huge security hole since whatever your 
user enters will be executed as Python code - thats what eval() does. 
Your user can ytype anything in there.


The correct approach is to use raw_input (since you seem to be using 
Pthon v2) and a conversion function such as int() to get the data

type that you want.



while userAccountNumber > 1 or <=999:


Python sees this as:

   while (userAccountNumber > 1) or <=999:

Which makes no sense.

In most programming languages you test this like this:

(userAccountNumber > 1) or (userAccountNumber <= 999):

But in Python you can do these compound checks more
succinctly like this:

> while not (999 < userAccountNumber < 1):

And the other way is to check the length of the input at source:

userAccount = ''
while len(userAccount) != 8:
   userAccount = raw_input("Please Input Your Account Number:")
userAccountNumber = int(userAccount)


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] While Loop?

2014-05-14 Thread Danny Yoo
On Wed, May 14, 2014 at 2:45 AM, Sam Ball  wrote:
> I'm attempting to create a program where the user inputs their account
> number (which must be 8 digits) and if what the user enters is not 8 digits
> in length I want python to tell the user this is invalid and then keep
> asking for the account number until a suitable number has been entered.
>  I'm guessing it's a sort of while loop but I'm not 100% sure as I'm quite
> new to Python.
>
> So far I have...
>
>
>
> userAccountNumber = eval(input("Please Input Your Account Number:"))


Don't use eval here.  If you want to turn a string that's full of
digits into a number, use int()

https://docs.python.org/3/library/functions.html#int

eval() is dangerous.  Don't use it.



> while userAccountNumber > 1 or <=999:
> print userAccountNumber ("Invalid Account Number! Account Must Be Eight
> Digits")
>
> When I run this however it gives me an invalid syntax and highlights the =
> in <=999:
>
> Would appreciate any help with sorting this out.



The compiler is technically correct: it's invalid syntax.

You've written something that scans as English.  But it does not scan
as a Python program.  "or" does not have the English meaning in
Python.

To put it another way: what are you comparing to be less than or equal
to ?  You've left that implicit.  Many programming languages
do not allow implicit statements because it's too easy to
misunderstand or misinterpret.

Make what you're comparing explicit.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for the error about 'import site' failed; use -v for traceback

2014-05-14 Thread C Smith
That looks pretty normal. I don't see any errors.

On Wed, May 14, 2014 at 5:42 AM, Tao Zhu  wrote:
> Hi everyone,
> when I use python, the problem occured. when I used the command "python -v",
> the results are listed as follows. could you tell me what wrong?
>
> $ python -v
> # installing zipimport hook
> import zipimport # builtin
> # installed zipimport hook
> # /usr/local/lib/python2.4/site.pyc matches /usr/local/lib/python2.4/site.py
> import site # precompiled from /usr/local/lib/python2.4/site.pyc
> # /usr/local/lib/python2.4/os.pyc matches /usr/local/lib/python2.4/os.py
> import os # precompiled from /usr/local/lib/python2.4/os.pyc
> import posix # builtin
> # /usr/local/lib/python2.4/posixpath.pyc matches
> /usr/local/lib/python2.4/posixpath.py
> import posixpath # precompiled from /usr/local/lib/python2.4/posixpath.pyc
> # /usr/local/lib/python2.4/stat.pyc matches /usr/local/lib/python2.4/stat.py
> import stat # precompiled from /usr/local/lib/python2.4/stat.pyc
> # /usr/local/lib/python2.4/UserDict.pyc matches
> /usr/local/lib/python2.4/UserDict.py
> import UserDict # precompiled from /usr/local/lib/python2.4/UserDict.pyc
> # /usr/local/lib/python2.4/copy_reg.pyc matches
> /usr/local/lib/python2.4/copy_reg.py
> import copy_reg # precompiled from /usr/local/lib/python2.4/copy_reg.pyc
> # /usr/local/lib/python2.4/types.pyc matches
> /usr/local/lib/python2.4/types.py
> import types # precompiled from /usr/local/lib/python2.4/types.pyc
> # /usr/local/lib/python2.4/warnings.pyc matches
> /usr/local/lib/python2.4/warnings.py
> import warnings # precompiled from /usr/local/lib/python2.4/warnings.pyc
> # /usr/local/lib/python2.4/linecache.pyc matches
> /usr/local/lib/python2.4/linecache.py
> import linecache # precompiled from /usr/local/lib/python2.4/linecache.pyc
> import encodings # directory /usr/local/lib/python2.4/encodings
> # /usr/local/lib/python2.4/encodings/__init__.pyc matches
> /usr/local/lib/python2.4/encodings/__init__.py
> import encodings # precompiled from
> /usr/local/lib/python2.4/encodings/__init__.pyc
> # /usr/local/lib/python2.4/codecs.pyc matches
> /usr/local/lib/python2.4/codecs.py
> import codecs # precompiled from /usr/local/lib/python2.4/codecs.pyc
> import _codecs # builtin
> # /usr/local/lib/python2.4/encodings/aliases.pyc matches
> /usr/local/lib/python2.4/encodings/aliases.py
> import encodings.aliases # precompiled from
> /usr/local/lib/python2.4/encodings/aliases.pyc
> # /usr/local/lib/python2.4/encodings/utf_8.pyc matches
> /usr/local/lib/python2.4/encodings/utf_8.py
> import encodings.utf_8 # precompiled from
> /usr/local/lib/python2.4/encodings/utf_8.pyc
> Python 2.4.4 (#3, May 15 2014, 00:22:35)
> [GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> dlopen("/usr/local/lib/python2.4/lib-dynload/readline.so", 2);
> import readline # dynamically loaded from
> /usr/local/lib/python2.4/lib-dynload/readline.so

> Tao
>
>
>
> ___
> 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] for the error about 'import site' failed; use -v for traceback

2014-05-14 Thread C Smith
What are you trying to do?

On Wed, May 14, 2014 at 12:24 PM, C Smith  wrote:
> That looks pretty normal. I don't see any errors.
>
> On Wed, May 14, 2014 at 5:42 AM, Tao Zhu  wrote:
>> Hi everyone,
>> when I use python, the problem occured. when I used the command "python -v",
>> the results are listed as follows. could you tell me what wrong?
>>
>> $ python -v
>> # installing zipimport hook
>> import zipimport # builtin
>> # installed zipimport hook
>> # /usr/local/lib/python2.4/site.pyc matches /usr/local/lib/python2.4/site.py
>> import site # precompiled from /usr/local/lib/python2.4/site.pyc
>> # /usr/local/lib/python2.4/os.pyc matches /usr/local/lib/python2.4/os.py
>> import os # precompiled from /usr/local/lib/python2.4/os.pyc
>> import posix # builtin
>> # /usr/local/lib/python2.4/posixpath.pyc matches
>> /usr/local/lib/python2.4/posixpath.py
>> import posixpath # precompiled from /usr/local/lib/python2.4/posixpath.pyc
>> # /usr/local/lib/python2.4/stat.pyc matches /usr/local/lib/python2.4/stat.py
>> import stat # precompiled from /usr/local/lib/python2.4/stat.pyc
>> # /usr/local/lib/python2.4/UserDict.pyc matches
>> /usr/local/lib/python2.4/UserDict.py
>> import UserDict # precompiled from /usr/local/lib/python2.4/UserDict.pyc
>> # /usr/local/lib/python2.4/copy_reg.pyc matches
>> /usr/local/lib/python2.4/copy_reg.py
>> import copy_reg # precompiled from /usr/local/lib/python2.4/copy_reg.pyc
>> # /usr/local/lib/python2.4/types.pyc matches
>> /usr/local/lib/python2.4/types.py
>> import types # precompiled from /usr/local/lib/python2.4/types.pyc
>> # /usr/local/lib/python2.4/warnings.pyc matches
>> /usr/local/lib/python2.4/warnings.py
>> import warnings # precompiled from /usr/local/lib/python2.4/warnings.pyc
>> # /usr/local/lib/python2.4/linecache.pyc matches
>> /usr/local/lib/python2.4/linecache.py
>> import linecache # precompiled from /usr/local/lib/python2.4/linecache.pyc
>> import encodings # directory /usr/local/lib/python2.4/encodings
>> # /usr/local/lib/python2.4/encodings/__init__.pyc matches
>> /usr/local/lib/python2.4/encodings/__init__.py
>> import encodings # precompiled from
>> /usr/local/lib/python2.4/encodings/__init__.pyc
>> # /usr/local/lib/python2.4/codecs.pyc matches
>> /usr/local/lib/python2.4/codecs.py
>> import codecs # precompiled from /usr/local/lib/python2.4/codecs.pyc
>> import _codecs # builtin
>> # /usr/local/lib/python2.4/encodings/aliases.pyc matches
>> /usr/local/lib/python2.4/encodings/aliases.py
>> import encodings.aliases # precompiled from
>> /usr/local/lib/python2.4/encodings/aliases.pyc
>> # /usr/local/lib/python2.4/encodings/utf_8.pyc matches
>> /usr/local/lib/python2.4/encodings/utf_8.py
>> import encodings.utf_8 # precompiled from
>> /usr/local/lib/python2.4/encodings/utf_8.pyc
>> Python 2.4.4 (#3, May 15 2014, 00:22:35)
>> [GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> dlopen("/usr/local/lib/python2.4/lib-dynload/readline.so", 2);
>> import readline # dynamically loaded from
>> /usr/local/lib/python2.4/lib-dynload/readline.so
>
>> Tao
>>
>>
>>
>> ___
>> 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] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text

2014-05-14 Thread Charles Agriesti
Practical programming, 2nd Edition, Paul Gries, Jennifer Campbell, Jason 
Montojo

(Python 3)

P 184, the last half of chapter 10 requires the time_series module, 
which is no longer available, apparently replaced by Pandas.


Looked into installing Pandas. Installing Anaconda is supposed to 
accomplish that, or help with it.
Install Anaconda, It contains its own Python27, requires uninstall of 
old Python 2 . Done.
Anaconda website says it allows easy switching between Python26, 
Python27, Python33. I have Python34 installed.

Windows Powershell
C:\Users\Charles
enter: < conda >
(shows a help file which I found less than helpful)
C:\Users\Charles
enter: < ipython notebook>

This opens a browser window  at  http://127.0.0.1:/   , clicking 
'new notebook' opens an unfamiliar looking ide called ipython notebook.


searching hard drive for pandas found a couple of things that I don't 
think are the pandas program. same for scipy and numpy.


Is this Anaconda thing any part of being able to run the scripts from 
the textbook with time_series? Is it a complete wild goose chase?
Should I install SciPy? Is Pandas separate from that? Should I install 
Python33? Will this conflict with the 27 and 34 already on this computer?

Give up on the Gries book and try a different one?
Thanks in advance if anybody can help.

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


Re: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text

2014-05-14 Thread Albert-Jan Roskam



> From: Charles Agriesti 
>To: Tutor@python.org 
>Sent: Wednesday, May 14, 2014 9:08 PM
>Subject: [Tutor] substituting for time_series, Pandas, Anaconda. Practical 
>Programming, intro Comp Sci, Gries text
> 
>
>Practical programming, 2nd Edition, Paul Gries, Jennifer Campbell, Jason 
>Montojo
>(Python 3)
>
>P 184, the last half of chapter 10 requires the time_series module, 
>which is no longer available, apparently replaced by Pandas.
>
>Looked into installing Pandas. Installing Anaconda is supposed to 
>accomplish that, or help with it.
>Install Anaconda, It contains its own Python27, requires uninstall of 
>old Python 2 . Done.
>Anaconda website says it allows easy switching between Python26, 
>Python27, Python33. I have Python34 installed.
>Windows Powershell
>C:\Users\Charles
>enter: < conda >
>(shows a help file which I found less than helpful)
>C:\Users\Charles
>enter: < ipython notebook>
>
>This opens a browser window  at  http://127.0.0.1:/   , clicking 
>'new notebook' opens an unfamiliar looking ide called ipython notebook.


Ipython = interactive python. Ipython notebook allows you to save interactive 
Python sessions as html. So you see input and output in one html file. Try 
entering "import pandas", and then "help(pandas)"


>searching hard drive for pandas found a couple of things that I don't 
>think are the pandas program. same for scipy and numpy.


Pandas uses the numpy library, among others.


>Is this Anaconda thing any part of being able to run the scripts from 
>the textbook with time_series? Is it a complete wild goose chase?
>Should I install SciPy? Is Pandas separate from that? Should I install 
>Python33? Will this conflict with the 27 and 34 already on this computer?
>Give up on the Gries book and try a different one?
>Thanks in advance if anybody can help.

Anaconda is a bundle of useful python goodies, so next to a basic python 
installation (which contains tons of cool stuff already), even more, 
non-standard libraries are installed. Like numoy, scipy, pandas, numba, etc etc.

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


Re: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text

2014-05-14 Thread Marc Tompkins
On Wed, May 14, 2014 at 12:08 PM, Charles Agriesti
wrote:


> Is this Anaconda thing any part of being able to run the scripts from the
> textbook with time_series? Is it a complete wild goose chase?
>


First off - I know nothing about using Python in a scientific setting.
Second (before anyone else points this out) this is the Python tutor list;
your question is a bit off-topic (i.e. relating to specific
packages/distributions rather than Python itself.)  This is not a criticism
or rebuke; it's just that you're likely to get better help on a list where
most of the people actually use the stuff you're asking about.

Having said that, Pandas appears to be a pretty normal Python package,
meaning that you could install it by itself; Anaconda is a distribution of
scientific Python that aims to do it all for you in one swell foop - but
it's not necessary.  If you've installed pip, it's as simple as "pip
install pandas".  If you haven't installed pip and you use Windows, the
simplest thing is to install pip-Win:
 https://sites.google.com/site/pydatalog/python/pip-for-windows

Anaconda actually installs a virtual environment that doesn't rely on or
interfere with other versions of Python that are already installed, so no
worries there.  As long as you work inside Anaconda, the fact that you also
have 3.4 installed elsewhere is not an issue.  The fact that Anaconda
presents you with a radically different working environment than you're
used to - that might be an issue.



> Should I install SciPy? Is Pandas separate from that?


Can't answer that.  SciPy is a "stack", meaning that it contains multiple
packages meant to work together (Pandas is one of them), and the SciPy
people themselves recommend Anaconda as one of the easiest ways to install
SciPy.  Do you need it?  No idea.

Should I install Python33? Will this conflict with the 27 and 34 already on
> this computer?
>

The chief source of conflict would be file associations and the system path
- i.e. what happens when you double-click on a .py file or type "python" at
a prompt.  Here's where Python's "virtualenv" comes in handy, or a Python
distribution that uses it - like Anaconda.  As long as you work inside
Anaconda, you don't have to worry about other versions.


> Give up on the Gries book and try a different one?
>

It seems well-reviewed; a discontinued package doesn't seem like a good
reason to abandon it, as long as there's a workaround...

It does look like Pandas is the replacement for scikits.timeseries
(although you're writing that as "time_series", which makes me wonder
whether it's the same package at all!)  Only trial and error will tell
whether the syntax for using Pandas is the same as what's in your book.
Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text

2014-05-14 Thread Jim Byrnes

On 05/14/2014 02:08 PM, Charles Agriesti wrote:

Practical programming, 2nd Edition, Paul Gries, Jennifer Campbell, Jason
Montojo
(Python 3)

P 184, the last half of chapter 10 requires the time_series module,
which is no longer available, apparently replaced by Pandas.


Read the first paragraph on page 185. The two def's on page 184 are 
saved in a file called time_series.py, this is what is being imported. 
Of course the paragraph gets confusing when it talks about importing 
tsdl.  If I remember right once the time_series file was in the current 
directory the examples then worked.




Regards,  Jim

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


Re: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text

2014-05-14 Thread Albert-Jan Roskam





- Original Message -
> From: Jim Byrnes 
> To: tutor@python.org
> Cc: 
> Sent: Wednesday, May 14, 2014 10:40 PM
> Subject: Re: [Tutor] substituting for time_series, Pandas, Anaconda. 
> Practical Programming, intro Comp Sci, Gries text
> 
> On 05/14/2014 02:08 PM, Charles Agriesti wrote:
>>  Practical programming, 2nd Edition, Paul Gries, Jennifer Campbell, Jason
>>  Montojo
>>  (Python 3)
>> 
>>  P 184, the last half of chapter 10 requires the time_series module,
>>  which is no longer available, apparently replaced by Pandas.
> 
> Read the first paragraph on page 185. The two def's on page 184 are 
> saved in a file called time_series.py, this is what is being imported. 
> Of course the paragraph gets confusing when it talks about importing 
> tsdl.  If I remember right once the time_series file was in the current 
> directory the examples then worked.

pandas = PANel DAta analysis (not sure about the S). So time series, orignally. 
CRAN R on steroids.

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


Re: [Tutor] While Loop?

2014-05-14 Thread Dave Angel

On 05/14/2014 05:45 AM, Sam Ball wrote:

I'm attempting to create a program where the user inputs their account number 
(which must be 8 digits) and if what the user enters is not 8 digits in length 
I want python to tell the user this is invalid and then keep asking for the 
account number until a suitable number has been entered.
  I'm guessing it's a sort of while loop but I'm not 100% sure as I'm quite new 
to Python.



Welcome to Python, and to the Tutor mailing list.  Please post in text 
form, as the html you're now using is quite error prone, especially 
since this is a text list.  There's no errors I can see with your 
present message, but it's a good habit to learn early - tell your email 
program to use "plain text".


Next, it's very important to specify what version of Python you're 
using.  In particular there are differences between Python version 2.x 
and version 3.x   And those differences affect the input statement.


From your print syntax, it appears you're aiming at Python 2.x  But 
it's not valid there either, so it's better if you be explicit in your 
question.




So far I have...




userAccountNumber = eval(input("Please Input Your Account Number:"))


Since I'm asssuming Python 2.x, you want   int(raw_input(   instead. 
input() in 2.x is a security mistake, and eval() is for either version 
of Python.




while userAccountNumber > 1 or <=999:


Syntax error, as you've discovered.  Alan has told you how to fix it. 
But it would be much better to get in the habit of quoting the entire 
error message (it's called a traceback) instead of paraphrasing it.



 print userAccountNumber ("Invalid Account Number! Account Must Be Eight 
Digits")


Once you've fixed the above, you'll get an error on this line.  But how 
to fix it depends on what Python version you're using. 
userAccountNumber isn't a function, so why are you trying to call it?


Next problem is that your loop doesn't ask the user again, it just loops 
around trying to print its error message.




When I run this however it gives me an invalid syntax and highlights the = in 
<=999:

Would appreciate any help with sorting this out.



Once you've gone as far as you can get with these suggestions from Alan, 
Danny, and me, be sure and use reply-list to post your next revision and 
query.  (Or, if your email program doesn't support that, use Reply-All, 
and remove from the header everyone you don't want to get the response). 
 And of course, you'll tell us what version of Python you're 
taqrgeting.  In particular, keep the tutor@python.org recipient.


Once your program works, you need to consider other things, possible 
bugs.  For example, what happends if a user types  "charlie" instead of 
a number?  Or if they type a floating point value?  What would you like 
to happen?


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


[Tutor] (no subject)

2014-05-14 Thread JEAN MICHEL
I'm a Python beginner trying write a program that reads outside txt files,
takes the data like the name and test grades of students then calculate the
average and also assigns a grade and writes the data into a new txt file.
I'm having difficulties writing the program so far I've been able to write
up half of the program but I am noticing there might be bugs. I've tried
running the program but every time I do, my program output is blank and
there is no error messages
here is the program

def calcaverage(test1,test2,test3):
for count in range(line):
curraverage=0
curraverage=((test1[count]+ test2[count]+ test3[count])/3)
currentaverage.append(curraverage)
if curraverage>= 90:
grade= "A"
lettergrades.append(grade)
else:
if curraverage >= 80 and curraverage < 90:
grade= "B"
lettergrades.append(grade)
else:
if curraverage >= 70 and curraverage < 80:
grade= "C"
lettergrades.append(grade)
else:
if curraverage < 70:
grade= "F"
lettergrades.append(grade)
name=[]
test1=[]
test2=[]
test3=[]
averagescore=[]
lettergrades=[]
with open ('/period1.txt', 'r') as infile:
line = infile.readline()
while line in infile:
values = line.split()
name.append(values[0] + ','+ values[1])
while line in infile:
values = line.split()
 score1=float(value[2])
test1.append(score1)
while line in infile:
values = line.split()
  score2=float(value[3])
test2.append(score2)
while line in infile:
values = line.split()
 score3=float(value[4])
test3.append(score3)
averagescore=calcaverage(test1,test2,test3)
infile.close()
print(line)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Translator - multiple choice answer

2014-05-14 Thread Mario Py
Hi all, my first post here and my first ever programing project! 
(actually stealing code from all over the internet for now)
So please be easy on me & hints probably will not work with me, I'm 
complete beginner...


I'm trying to create something useful, small program which will help my 
wife to refresh her foreign knowledge of once already memorized words.

Program works. But I would like to expand it a little bit.

Program read TXT file (c:\\slo3.txt)
In this file there are two words per line separated by tab.
First word is foreign language and second word is proper translation, 
like this:


pivobeer
kruhbread
rdeca   red
krompir potatoe
hisahouse
cesta   road
autocar

(not even trying to mess with special characters for now, lol)

I was going to read content into dictionary, each pair as tuple but I 
gave up, couldn't figure it out. Looks like it is working with the list 
so no problem.


Question 1: would be better to use dictionary, than list?

Question 2: slo3.txt is just small sample for now and before I type in 
all words, I would like to know is it better to use some other separator 
such as coma or empty space instead of TAB? I found on the internet 
example for TAB only, so this is what I'm using for now.


OK here is working code:

from random import shuffle
print('Write translation of Slovene word ')
print()

with open('c:\\slo3.txt') as f:
lines = f.readlines()

shuffle(lines)

for line in lines:
question, rightAnswer = line.strip().split('\t') # words are two 
per line separated by TAB.

answer = input(question + ' ')
if answer.lower() == rightAnswer:
a = 0# please ingore this for now, IF wants something 
here and I don't want to print extra line, output looks OK for now...

# print()
else:
print('Wrong, correct is: %s.' % rightAnswer,)
print()

I need help with two things. First one is simple, basic, but I couldn't 
figure it out. If I want to print out 'Wrong' part in the same line 
next to wrong answer, how do I do it?


Now, big, huge help request.
I would like to make it easy on my wife :-) instead of her needing to 
type in answer I would like that she could choose (click on) multiple 
choice. Say she get 4 or 5 possible answers and one of them is correct. 
Then she need to click on correct answer...


What do I need to do? I understand there will be some graphic/windows 
things involved. I don't have any additional packages or libraries 
installed, nor do I know what/how do do it. Complete noob


I'm using Python 3.4 and Windows 7.

Mario

P.s.
I'm not even sure if this will show up on Tutor's forum...
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2014-05-14 Thread Alan Gauld

On 14/05/14 21:48, JEAN MICHEL wrote:

I'm a Python beginner


Welcome to the list.

Please always include a subject line relating to your question.
It makes searching the archives much easier!


 I'm having difficulties writing the program so far I've
been able to write up half of the program but I am noticing there might
be bugs.


Get used to it, it's impossible to write more than a
few lines of code without introducing bugs.

The trick is to get rid of them before you give the code
to anyone else! ;-)

I've tried running the program but every time I do, my program

output is blank and there is no error messages


Try inserting some print statements to track where you have reached.
For example one just as you enter your function could show you
how often the function is being called and with what values
Although you only seem to call it once I notice...

Thee are a couple of things below that can drive you crazy,
please don't do them.
See comments below


def calcaverage(test1,test2,test3):
 for count in range(line):


Where is line coming from? What is its value.
Maybe a print here would surprise you?
And what does range(line) produce?
Again maybe not what you expect?

If you want to use a value in a function pass
it in as a parameter...


 curraverage=0


This does nothing useful since you overwrite
curraverage in the very next line.


 curraverage=((test1[count]+ test2[count]+ test3[count])/3)
 currentaverage.append(curraverage)


Where is currentaverage defined? Apparently its a list
but I don't see it below...


 if curraverage>= 90:
 grade= "A"
 lettergrades.append(grade)
 else:
 if curraverage >= 80 and curraverage < 90:


You can save a little typing by saying this as:

  if 80 <= curraverage < 90:




 grade= "B"
 lettergrades.append(grade)
 else:
 if curraverage >= 70 and curraverage < 80:


And you can save some more as well as some indenting by using elif: 
instead of else:... if:



name=[]
test1=[]
test2=[]
test3=[]
averagescore=[]
lettergrades=[]
with open ('/period1.txt', 'r') as infile:
 line = infile.readline()
 while line in infile:


The easiest way to process lines in Python is to
use a for loop:

for line in infile:


 values = line.split()
 name.append(values[0] + ','+ values[1])
 while line in infile:


This definition of line will overwrite the value you had previously.
Is that really what you want? Using the same variable to hold two 
different values in a single loop is usually a bad idea.





 values = line.split()
  score1=float(value[2])
 test1.append(score1)
 while line in infile:


And doing it 3 times is just asking for trouble!
Especially since you are trying to iterate over
the same file each time!


 values = line.split()
   score2=float(value[3])
 test2.append(score2)
 while line in infile:


And now 4 times. Eeek! How do you keep track
of what line's value is?

I suspect you need to unwind all these loops and rethink
the structure here. Too complicated for me to try
and read...


 values = line.split()
  score3=float(value[4])
 test3.append(score3)
averagescore=calcaverage(test1,test2,test3)
infile.close()


You don't need to close a file you opened using 'with'
Part of the 'with' magic is that it closes the file for you.


print(line)


I suspect this print is way to late to help you.

Try printing line (along with a label saying which
while loop it is) after each while statement.

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] Translator - multiple choice answer

2014-05-14 Thread Danny Yoo
> Program read TXT file (c:\\slo3.txt)
> In this file there are two words per line separated by tab.
> First word is foreign language and second word is proper translation, like
> this:
>
> pivobeer
> kruhbread
> rdeca   red
> krompir potatoe
> hisahouse
> cesta   road
> autocar
>
> (not even trying to mess with special characters for now, lol)

Do look at:

http://www.joelonsoftware.com/articles/Unicode.html

because the fact that you're dealing with foreign language means you
want to get it right.


If it were me, I'd just say that the input is UTF-8 encoded text, and
always open up the file in utf-8 mode.

myfile = open("slo3.txt", "r", encoding="utf8")

and just know that we're working with Unicode from that point forward.



> I was going to read content into dictionary, each pair as tuple but I gave
> up, couldn't figure it out. Looks like it is working with the list so no
> problem.
>
> Question 1: would be better to use dictionary, than list?

It depends.

If you're picking out a random entry, then having a dictionary in hand
is not going to need the key lookup support that dictionaries give
you.

For the application you're describing right now, it doesn't sound like
you need this yet.



> Question 2: slo3.txt is just small sample for now and before I type in all
> words, I would like to know is it better to use some other separator such as
> coma or empty space instead of TAB? I found on the internet example for TAB
> only, so this is what I'm using for now.

TAB is a reasonable separator.  You might also consider comma, as in
Comma-Separated Values (CSV).

If your data starts having more structure, then check back with folks
on the tutor mailing list.  There are richer formats you can use, but
your program's description suggests that you probably don't need the
complexity yet.




>
> I need help with two things. First one is simple, basic, but I couldn't
> figure it out. If I want to print out 'Wrong' part in the same line next
> to wrong answer, how do I do it?

The print function puts a newline at the end.  You can change this
default behavior by providing an "end" keyword to it.  The
documentation mentions it here:

https://docs.python.org/3/library/functions.html#print

Small test program to demonstrate:


print("Hello ", end="")
print("world ")




> Now, big, huge help request.
> I would like to make it easy on my wife :-) instead of her needing to type
> in answer I would like that she could choose (click on) multiple choice. Say
> she get 4 or 5 possible answers and one of them is correct. Then she need to
> click on correct answer...
>
> What do I need to do? I understand there will be some graphic/windows things
> involved. I don't have any additional packages or libraries installed, nor
> do I know what/how do do it. Complete noob

How about printing them with numbers, so that entry is just a number
rather than the typed word?


You can put a graphical user interface on the program, though it does
take a bit more effort to get it to work.

Look into "Tkinter", which is a library for producing graphical user interfaces:

https://wiki.python.org/moin/TkInter



Another option might be to turn your program into a web site, so that
the interface is the web browser, which everyone is getting used to
these days.  But this, too, is also... involved.  :P
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translator - multiple choice answer

2014-05-14 Thread C Smith
This might be useful for reading values from a text value into a dictionary:
https://stackoverflow.com/questions/17775273/how-to-read-and-store-values-from-a-text-file-into-a-dictionary-python

On Wed, May 14, 2014 at 7:00 PM, Danny Yoo  wrote:
>> Program read TXT file (c:\\slo3.txt)
>> In this file there are two words per line separated by tab.
>> First word is foreign language and second word is proper translation, like
>> this:
>>
>> pivobeer
>> kruhbread
>> rdeca   red
>> krompir potatoe
>> hisahouse
>> cesta   road
>> autocar
>>
>> (not even trying to mess with special characters for now, lol)
>
> Do look at:
>
> http://www.joelonsoftware.com/articles/Unicode.html
>
> because the fact that you're dealing with foreign language means you
> want to get it right.
>
>
> If it were me, I'd just say that the input is UTF-8 encoded text, and
> always open up the file in utf-8 mode.
>
> myfile = open("slo3.txt", "r", encoding="utf8")
>
> and just know that we're working with Unicode from that point forward.
>
>
>
>> I was going to read content into dictionary, each pair as tuple but I gave
>> up, couldn't figure it out. Looks like it is working with the list so no
>> problem.
>>
>> Question 1: would be better to use dictionary, than list?
>
> It depends.
>
> If you're picking out a random entry, then having a dictionary in hand
> is not going to need the key lookup support that dictionaries give
> you.
>
> For the application you're describing right now, it doesn't sound like
> you need this yet.
>
>
>
>> Question 2: slo3.txt is just small sample for now and before I type in all
>> words, I would like to know is it better to use some other separator such as
>> coma or empty space instead of TAB? I found on the internet example for TAB
>> only, so this is what I'm using for now.
>
> TAB is a reasonable separator.  You might also consider comma, as in
> Comma-Separated Values (CSV).
>
> If your data starts having more structure, then check back with folks
> on the tutor mailing list.  There are richer formats you can use, but
> your program's description suggests that you probably don't need the
> complexity yet.
>
>
>
>
>>
>> I need help with two things. First one is simple, basic, but I couldn't
>> figure it out. If I want to print out 'Wrong' part in the same line next
>> to wrong answer, how do I do it?
>
> The print function puts a newline at the end.  You can change this
> default behavior by providing an "end" keyword to it.  The
> documentation mentions it here:
>
> https://docs.python.org/3/library/functions.html#print
>
> Small test program to demonstrate:
>
> 
> print("Hello ", end="")
> print("world ")
> 
>
>
>
>> Now, big, huge help request.
>> I would like to make it easy on my wife :-) instead of her needing to type
>> in answer I would like that she could choose (click on) multiple choice. Say
>> she get 4 or 5 possible answers and one of them is correct. Then she need to
>> click on correct answer...
>>
>> What do I need to do? I understand there will be some graphic/windows things
>> involved. I don't have any additional packages or libraries installed, nor
>> do I know what/how do do it. Complete noob
>
> How about printing them with numbers, so that entry is just a number
> rather than the typed word?
>
>
> You can put a graphical user interface on the program, though it does
> take a bit more effort to get it to work.
>
> Look into "Tkinter", which is a library for producing graphical user 
> interfaces:
>
> https://wiki.python.org/moin/TkInter
>
>
>
> Another option might be to turn your program into a web site, so that
> the interface is the web browser, which everyone is getting used to
> these days.  But this, too, is also... involved.  :P
> ___
> 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] (no subject)

2014-05-14 Thread Dave Angel

On 05/14/2014 04:48 PM, JEAN MICHEL wrote:

I'm a Python beginner


Welcome to python-tutor


trying write a program that reads outside txt files,


What's an "outside txt file"?  And since you only one file, perhaps you 
left out the othertwo?




takes the data like the name and test grades of students then calculate the
average and also assigns a grade and writes the data into a new txt file.
I'm having difficulties writing the program so far I've been able to write
up half of the program but I am noticing there might be bugs. I've tried
running the program but every time I do, my program output is blank and
there is no error messages


I'm quite surprised at that, since you're calling range with a parameter 
of line, and line is some text string, not an int.



here is the program

def calcaverage(test1,test2,test3):
 for count in range(line):
 curraverage=0
 curraverage=((test1[count]+ test2[count]+ test3[count])/3)
 currentaverage.append(curraverage)
 if curraverage>= 90:
 grade= "A"
 lettergrades.append(grade)
 else:
 if curraverage >= 80 and curraverage < 90:
 grade= "B"
 lettergrades.append(grade)
 else:
 if curraverage >= 70 and curraverage < 80:
 grade= "C"
 lettergrades.append(grade)
 else:
 if curraverage < 70:
 grade= "F"
 lettergrades.append(grade)


You forgot to start a function here.  Thus all the following code is at 
top=level, a poor practice.



name=[]
test1=[]
test2=[]
test3=[]
averagescore=[]
lettergrades=[]
with open ('/period1.txt', 'r') as infile:
 line = infile.readline()


If you're using this statement to skip the first line of the file, you 
should not bother to assign the result anywhere.  Or assign it to a 
meaningful variable like  "ignore"


 ignore = infile.readline()


 while line in infile:
 values = line.split()
 name.append(values[0] + ','+ values[1])
 while line in infile:


Whoops.  By the time this loop finishes, the outer one will find there 
are no more lines left in the file.



 values = line.split()
  score1=float(value[2])
 test1.append(score1)
 while line in infile:
 values = line.split()
   score2=float(value[3])
 test2.append(score2)
 while line in infile:
 values = line.split()
  score3=float(value[4])
 test3.append(score3)


This is the place where you should call the above data-gathering 
function (the one you didn't make a function)


Then once you've gathered the data, you should call calcaverage() with 
test1, test2,test3, and at least one more argument.



averagescore=calcaverage(test1,test2,test3)
infile.close()


Not needed.  infile is closed once the with clause completes.



print(line)


Which of the many meanings of line do you mean to print out??

Those triply nested reads of the same file are doomed to failure.  Once 
the innermost one of them reaches the end of file, the others will see 
nothing more.


We can't debug it much further without knowing much more about what the 
data file (files ???) looks like.  For example, perhaps there are 
supposed to be three files, corresponding to the three lists: test1, 
test2, and test3.


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


Re: [Tutor] (no subject)

2014-05-14 Thread Danny Yoo
> if curraverage>= 90:
> grade= "A"
> lettergrades.append(grade)
> else:
> if curraverage >= 80 and curraverage < 90:
> grade= "B"
> lettergrades.append(grade)
> else:
> if curraverage >= 70 and curraverage < 80:
> grade= "C"
> lettergrades.append(grade)
> else:
> if curraverage < 70:
> grade= "F"
> lettergrades.append(grade)


Just wanted to note that this style of cascading if statements is a
little unusual here.  Since you know that only one of the tests is
going to be true, you can use a more parallel structure.

That is, if you're doing something like:

##
if test1:
   ...
   else:
if test2:
...
else:
if test3:
...
else:
...
##

and if you know that test1, test2, and test3 don't overlap, you can
express this as:


##
if test1:
...
elif test2:
...
elif test3:
...
else:
...
##

and avoid the Tower of Pisa-style code.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2014-05-14 Thread Danny Yoo
On Wed, May 14, 2014 at 9:15 PM, Danny Yoo  wrote:
>> if curraverage>= 90:
>> grade= "A"
>> lettergrades.append(grade)
>> else:
>> if curraverage >= 80 and curraverage < 90:
>> grade= "B"
>> lettergrades.append(grade)
>> else:
>> if curraverage >= 70 and curraverage < 80:
>> grade= "C"
>> lettergrades.append(grade)
>> else:
>> if curraverage < 70:
>> grade= "F"
>> lettergrades.append(grade)
>
>
> Just wanted to note that this style of cascading if statements is a
> little unusual here.  Since you know that only one of the tests is
> going to be true, you can use a more parallel structure.


Sorry, I had combined two ideas in my post, one of which (the
exclusivity hint) is totally not relevant in this situation.  We can
do the code transformation unconditionally.  The exclusivity of the
tests doesn't matter here.


So when we see ourselves doing:

##
if test1:
   ...
   else:
if test2:
...
else:
if test3:
...
else:
...
##

we should always be able to flatten this out to this shape instead.

##
if test1:
...
elif test2:
...
elif test3:
...
else:
...
##


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


Re: [Tutor] (no subject)

2014-05-14 Thread Cameron Simpson

Before we begin, two etiquette requests:

  - please supply a meaningful subject line; it helps everyone keep track of the 
discussions; this one might have used something like "help with simple grading 
problem"

  - please see if your mailer (GMail?) can be told to send just plain text to 
this list

Anyway, to the topic...

On 14May2014 13:48, JEAN MICHEL  wrote:

I'm a Python beginner trying write a program that reads outside txt files,
takes the data like the name and test grades of students then calculate the
average and also assigns a grade and writes the data into a new txt file.
I'm having difficulties writing the program so far I've been able to write
up half of the program but I am noticing there might be bugs. I've tried
running the program but every time I do, my program output is blank and
there is no error messages


In there circumstances it can be helpful to insert "print" statements in 
various places in the program to see what parts of it are actually being run, 
and with what values.



here is the program

def calcaverage(test1,test2,test3):
   for count in range(line):


The first thing that catches my eye is that "line" is not a variable in this 
function. You clearly mean it to have a value, but it is not supplied. Normally 
it would come in as a parameter with "test1, test2, test3".


On further inspection, you don't use "line", and the function does not return 
"grade".


In Python, all variables in a function are, generally, "local" variables; this 
is good practice in most other languages too - it means that you can look at a 
function on its own without knowing much about the rest of the program, and 
also means that what happens inside the function stay inside the function and 
does not accidentally affect the rest of the program.


It looks like your function here calculates values from the [count] element of 
the three test lists. So I would advocate:


  - get rid of the "for" loop - it belongs in your main program, not here

  - pass in "count" and "lettergrades" as parameters to the function - it needs 
to be given them

  - return "grade" from the function, by putting:

  return grade

at the bottom

[...snip...]

name=[]
test1=[]
test2=[]
test3=[]
averagescore=[]
lettergrades=[]
with open ('/period1.txt', 'r') as infile:


You may mean just "period1.txt" here? On a UNIX system "/period1.txt" would be 
at the top of the system directory tree, not in your current directory.



   line = infile.readline()
   while line in infile:


I suspect this is where your problem with no output lies.

I would expect you want to iterate over every line in the input file.

What you are actually doing above is two things:

  - reading the first line of the file, just once

  - examining the rest of the file to see if the line occurs later (presumably 
not)

Because an open text file is iterable, your while condition:

  line in infile

actually loads the rest of the file to see if "line" is "in" it. That reads 
everything. And becuase (probably) the line does not occur a second time, the 
while loop does not every run once, thus no output.


Replace these two lines with this:

  for line in infile:

which will read each line in turn for processing. Then see what happens.

Note that you do this again lower down. I am not sure what you intend to happen 
here. Put it "print" statements to see what actually happens - it should help 
you figure out what you really need to do.


[...snip...]

infile.close()


You do not need to "infile.close()". The original:

  with open ('/period1.txt', 'r') as infile:

will do that for you.


print(line)


This is at the bottom, outside all the loops. It can at best print the last 
line of the file. I do not know what you actaully intend here.


See if any of these suggestions help you progress with your program, and report 
back (by replying to this thread) when you have new problems or new questions.


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


Re: [Tutor] (no subject)

2014-05-14 Thread JEAN MICHEL
thank you for the all advice I really appreciate it. I probably wasn't
 wasn't too clear when i first sent my email asking for help, my apologies
so let me better explain what i'm doing. I'm trying to write a program that
reads 3 class data files such as period1.txt, period2.txt. Inside those
files are the first name, last name, and 3 test scores from 3 different
students.

The format of the data files looks like this
Meagan. Hesse. 99. 99. 99

 My program is then suppose to take the average assign a grade and write
the results and also the students names in another txt folder

I've tried fixing some of the problems but it looks like it's still a work
in progress I'm not to sure whether i'm opening the data files right, can
you take a look at that part


def calcaverage(test1,test2,test3):
for count in range(test1,test2,test3):
curraverage=0
curraverage=((test1[count]+ test2[count]+ test3[count])/3)
currentaverage.append(curraverage)
if curraverage>= 90:
grade= "A"
lettergrades.append(grade)
elif curraverage >= 80 and curraverage < 90:
grade= "B"
lettergrades.append(grade)
elif curraverage >= 70 and curraverage < 80:
grade= "C"
lettergrades.append(grade)
elif curraverage < 70:
grade= "F"
lettergrades.append(grade)

name=[]
test1=[]
test2=[]
test3=[]
averagescore=[]
lettergrades=[]

with open ("period1.txt", 'r') as infile:
for line in infile:
values = line.split()
name.append(values[0] + ','+ values[1])
for line in infile:
values = line.split()
score1=float(values[2])
test1.append(score1)
for line in infile:
values = line.split()
score2=float(values[3])
test2.append(score2)
for line in inline:
values = line.split()
score3=float(values[4])
test3.append(score3)
averagescore=calcaverage(test1,test2,test3)

print(line)



On Wed, May 14, 2014 at 9:15 PM, Danny Yoo  wrote:

> > if curraverage>= 90:
> > grade= "A"
> > lettergrades.append(grade)
> > else:
> > if curraverage >= 80 and curraverage < 90:
> > grade= "B"
> > lettergrades.append(grade)
> > else:
> > if curraverage >= 70 and curraverage < 80:
> > grade= "C"
> > lettergrades.append(grade)
> > else:
> > if curraverage < 70:
> > grade= "F"
> > lettergrades.append(grade)
>
>
> Just wanted to note that this style of cascading if statements is a
> little unusual here.  Since you know that only one of the tests is
> going to be true, you can use a more parallel structure.
>
> That is, if you're doing something like:
>
> ##
> if test1:
>...
>else:
> if test2:
> ...
> else:
> if test3:
> ...
> else:
> ...
> ##
>
> and if you know that test1, test2, and test3 don't overlap, you can
> express this as:
>
>
> ##
> if test1:
> ...
> elif test2:
> ...
> elif test3:
> ...
> else:
> ...
> ##
>
> and avoid the Tower of Pisa-style code.
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2014-05-14 Thread Alan Gauld

On 15/05/14 05:57, JEAN MICHEL wrote:


with open ("period1.txt", 'r') as infile:
 for line in infile:
 values = line.split()
 name.append(values[0] + ','+ values[1])
 for line in infile:
 values = line.split()
 score1=float(values[2])
 test1.append(score1)


I've just realised (I think) why you insist on nesting loops.
You are trying to get the lines in groups?

There are various ways to do this but the easiest here is
to use next():

with open(period.txt') as infile:
for line in infile:
   process first line
   line = next(infile)  # get second line
   process second line
   line = next(infile)  # get third line
   etc...

That way you don't read the whole file at one go which your
nested loop "solution" does. The for loop will effectively
read every 5th line (if you have 4 next() calls inside it)

But you may have to use a try/except clause to catch
a StopIteration(?) exception if the file does not have
a multiple of 5 lines in it.

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