Re: [Tutor] (no subject)

2013-11-22 Thread Peter Otten
久場海人 wrote:

> Hi. I began programming literally 2 days ago.
> 
> This is a code to setup password and confirms to make sure they both
> match, and I need to know what coding I can use to loop back to specific
> line to start the code over if the user were to incorrectly typed in the
> password.
> 
> 
> 1. CreatePassword = raw_input ("Create New Password: ")
> 2. ConfirmationPass = raw_input ("Retype Password: ")
> 3.
> 4.
> 5. if CreatePassword == ConfirmationPass:
> 6. print ("Password Set!")
> 7. else:
> 8. print ("Password did not match!")

Use an infinite loop and break out of that when you get the correct data. 
Example:

while True: # infinite loop
guess = raw_input("Guess my number: ")
if guess == "42":
print "correct"
break # break out of the infinite loop
else:
print "try again"

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


Re: [Tutor] Reading number x and printing number x+1

2013-11-22 Thread Peter Otten
G. McKinnon Ryall wrote:

> I have a script that outputs results to a file (one file, reused.) I would
> like to have an output file in this format
> 
> #
> (blank line)
> (output from program (only one line))
> name
> (T/F)
> (result iteration, shortened to x.)
> #-
> so like this
> #-
> 
> 55
> Joe
> false
> 1
> 
> 96
> Bob
> true
> 2
> 
> 28
> Mike
> true
> 3
> #-
> 
> I couldn't think of a way to read the last written number (it would be a
> multiple of 5). 

Just read the lines from the file, convert the last line to an int and add 
the new record. Example:

with open("tmp.txt", "a+") as f:
line = "0" # default when the file is empty or doesn't exist
for line in f:
pass
n = int(line) + 1
f.write("{}\n".format(n))


> I know how to add 1 and print. Also, how would I arrange
> #
> output.write("\n")
> output.write(x_string)
> output.write("\n")
> output.write(name)
> output.write("\n")
> output.write(exists_string)
> output.write("\n")
> #---
> so that it didn't show up like this
> FIRST TIME

Now you have the n from my above sample you can run some of the code 
conditionally:

if n == 1:
...
else:
...

> Also, how would I make this program better in general?

Long if elif sequences can sometimes be simplified with a dict lookup:

if a == 1:
   print "alpha"
elif a == 2:
   print "beta"
...

then becomes

messages = {1: "alpha", 2: "beta"}
try:
print messages[a]
except KeyError:
# handle cases not covered by the value lookup approach


Also, Python allows multiline strings:

f.write("alpha\n")
f.write("beta\n")

becomes

f.write("""\
alpha
beta
""")

> #BEGIN
> # Importing exists. Simple English. Nonsensical, but English nonetheless.
> from os.path import exists
> #Asking for your name
> name = raw_input("Hello!\nWhat is your name?\n\n")
> #Asking for the number and reacting to given response
> x = int(raw_input("\tFor the purposes of testing, please select a number
> between one and 100\n\n\t"))
> 
> if x < 0:
> x = 4554073
> print "\t\tNo negatives, asshole. Number changed to '4554073' for
> 'ASSHOLE', asshole."
> elif x == 0:
> print "\t\tMore than zero."
> elif x > 100:
> x = 101
> print "\t\tDon't fuck with the computer."
> elif x == 42:
> print "\t\tThe meaning of life, the universe and everything."
> elif x == 7:
> print "\t\t7. How... creative. *cough* UNORIGINAL *cough*"
> elif x == 3:
> print "\t\t3! It's a magic numba\'. Yes it is! It's a magic numba\'."
> elif x == 37:
> print "\t\tThe two most common numbers."
> elif x == 99:
> print "\t\tI got 99 problems and a- wait, no, that's your IQ."
> elif x == 27:
> print "\t\tCONGRATULATATIONS! YOU'VE FOUND MY FOAVORITE NOMBER!"
> else:
> print "In all aspects your number appears to be normal."
> #Changing name to a string
> name_string = "%s" % name
> #Changing x to a string
> x_string = "%s" % x
> #Checking if file exists for archival purposes
> exists = exists("number_output.txt")
> exists_string = "%s" % exists
> #Opening output file
> number_output = 'number_output.txt'
> output = open(number_output, 'a')
> #Writing to file
> output.write("\n")
> output.write(x_string)
> output.write("\n")
> output.write(name)
> output.write("\n")
> output.write(exists_string)
> output.write("\n")
> #END


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


Re: [Tutor] Issue w/ while loops

2013-11-22 Thread Rafael Knuth
>> I'm only stuck at one point: How do I loop back to the beginning in
>> case the user input is invalid?
>
>
> Look at Peter's example. He set a variable to false when the input was
> wrong. You can check that value in your while loop.

Ok, got you!

print("TIME TRACKING")

while True:
hours_worked = input("How many hours did you work today? ")
try:
hours_worked = float(hours_worked)
break
except ValueError:
print ("Invalid input")
if hours_worked < 24:
print("You must be a human.")
else:
print("You must be a cyborg.")

Program works well now, a learned a lot along the way.
Thank you & have a great weekend,

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


[Tutor] Sending sensible e-mail (was: Re: (no subject))

2013-11-22 Thread Dominik George
Hi,

> Subject: [Tutor] (no subject)

On a side note, please learn how to send e-mail.

Thanks,
Nik

-- 
* mirabilos is handling my post-1990 smartphone *
 Aaah, it vibrates! Wherefore art thou, demonic device??

PGP-Fingerprint: 3C9D 54A4 7575 C026 FB17  FD26 B79A 3C16 A0C4 F296


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


Re: [Tutor] Sending sensible e-mail (was: Re: (no subject))

2013-11-22 Thread Peter Otten
Dominik George wrote:

>> Subject: [Tutor] (no subject)
> 
> On a side note, please learn how to send e-mail.

Nik,

this is a beginners' list, so please be more constructive.

久場海人,

Nik may be unfriendly, but he is right; in future posts please take the time 
to pick a subject that gives the reader an idea of the problem you ran into.

Thank you.

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


Re: [Tutor] Is there a package to "un-mangle" characters?

2013-11-22 Thread Albert-Jan Roskam

 > Today I had a csv file in utf-8 encoding, but part of
 the accented
 > characters were mangled. The data were scraped from a
 website and it
 > turned out that at least some of the data were mangled
 on the website
 > already. Bits of the text were actually cp1252 (or
 cp850), I think,
 > even though the webpage was in utf-8 Is there any
 package that helps
 > to correct such issues?
 
 The links in the Wikipedia article may help:
 
     http://en.wikipedia.org/wiki/Charset_detection
 
 International Components for Unicode (ICU) does charset
 detection:
 
     http://userguide.icu-project.org/conversion/detection
 
 Python wrapper:
 
     http://pypi.python.org/pypi/PyICU
     http://packages.debian.org/wheezy/python-pyicu
 
 Example:
 
     import icu
 
     russian_text = u'Здесь некий
 текст на русском языке.'
     encoded_text =
 russian_text.encode('windows-1251')
 
     cd = icu.CharsetDetector()
     cd.setText(encoded_text)
     match = cd.detect()
     matches = cd.detectAll()
 
     >>> match.getName()
     'windows-1251'
     >>> match.getConfidence()
     33
     >>> match.getLanguage()
     'ru'
 
     >>> [m.getName() for m in matches]
     ['windows-1251', 'ISO-8859-6', 'ISO-8859-8-I',
 'ISO-8859-8']
     >>> [m.getConfidence() for m in
 matches]
     [33, 13, 8, 8]


> Hi Mark, Eryksun,

Thank you very much for your suggestions. Mark (sorry if I repeat myself but I 
think my earlier reply got lost), charset seems worth looking into. In 
hindsight I knew about chardet (with 'd'), I just forgot about it.  Re: your 
other remark: I think encoding issues are such a common phenomenon that one can 
never be too inexperienced to start reading about it.

The ICU module seems very cool too. I like the fact that you can even calculate 
a level of confidence. I wonder how it performs in my language (Dutch), where 
accented characters are not very common. 

Most is ascii (the printable chars in 0-128) and those are (I think) useless 
for trying to figure out the encoding. After all, utf-8, latin-1, cp1252, 
iso-8859-1 are all supersets of ascii. But in practice I treat those last three 
encodings as the same anyway (or was there some sneaky difference with 
fancyquotes?). 

I did a quick check and 0.2 % of the street names in my data (about 300K 
records) contain one or more accented characters (ordinals > 128). Since only 
part of the records are mangled, I may need to run getName() on every record 
that has accented characters in it.
 
Regards,
Albert-Jan
 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Two subsequent for loops in one function

2013-11-22 Thread Rafael Knuth
Hej there,

newbie question: I struggle to understand what exactly those two
subsequent for loops in the program below do (Python 3.3.0):

for x in range(2, 10):
for y in range(2, x):
if x % y == 0:
print(x, "equals", y, "*", x//y)
break
else:
print(x, "is a prime number")

The result is:

>>>
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

I have a very basic understanding of for loops, so for example:

for everything in range(10):
print(everything)

... the for loop grabs everything in that given range and prints it.
But I feel confused by the double use of for loops as show above.

Can anyone explain?

Thanks

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


Re: [Tutor] Two subsequent for loops in one function

2013-11-22 Thread Peter Otten
Rafael Knuth wrote:

> Hej there,
> 
> newbie question: I struggle to understand what exactly those two
> subsequent for loops in the program below do (Python 3.3.0):
> 
> for x in range(2, 10):
> for y in range(2, x):
> if x % y == 0:
> print(x, "equals", y, "*", x//y)
> break
> else:
> print(x, "is a prime number")
> 
> The result is:
> 

> 2 is a prime number
> 3 is a prime number
> 4 equals 2 * 2
> 5 is a prime number
> 6 equals 2 * 3
> 7 is a prime number
> 8 equals 2 * 4
> 9 equals 3 * 3
> 
> I have a very basic understanding of for loops, so for example:
> 
> for everything in range(10):
> print(everything)
> 
> ... the for loop grabs everything in that given range and prints it.
> But I feel confused by the double use of for loops as show above.
> 
> Can anyone explain?

Try to understand the inner for loop first. Once you understand what it does 
treat it as a black box like so:

def unknown(x):
for y in range(2, x):
if x % y == 0:
print(x, "equals", y, "*", x//y)
break
else:
print(x, "is a prime number")

for x in range(2, 10):
unknown(x)


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


Re: [Tutor] Two subsequent for loops in one function

2013-11-22 Thread Steven D'Aprano
On Fri, Nov 22, 2013 at 03:24:31PM +0100, Rafael Knuth wrote:
> Hej there,
> 
> newbie question: I struggle to understand what exactly those two
> subsequent for loops in the program below do (Python 3.3.0):
> 
> for x in range(2, 10):
> for y in range(2, x):
> if x % y == 0:
> print(x, "equals", y, "*", x//y)
> break
> else:
> print(x, "is a prime number")

The most tricky part here is, in my opinion, the "else" clause, because 
sadly it is badly named. It really should be called "then". A for-loop 
followed by an "else" means something like this:

for something in something:
run this block for each value
else:
then run this block


(Notice that the "else" lines up with the "for".)

What's the point of that? The point is that a "break" command jumps out 
of the *complete* for-else block. Try these examples:

for i in range(5):
if i == 400: break
print(i)
else:
print("Finished loop!")


for i in range(5):
if i == 4: break
print(i)
else:
print("Finished loop!")


So the "else" clause only runs after the for block provided you never 
break out of the loop.


Now, back to your nested loops. You have:

for x in range(2, 10):
for y in range(2, x):
... more code here ...


The "for x" loop runs with x=2, x=3, x=4, ... x=9. For each of those 
values, the block inside the loop runs. Okay, so what's inside the 
block? It runs another for-loop, in this case a for-y loop. This ought 
to be more clear if you run a simpler (and shorter) example:

for x in range(2, 7):
print("outer loop, x =", x)
for y in range(2, x):
print("inner loop, x =", x, "y =", y)


If you run that code, it should help you understand what the nested 
loops are doing.



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


Re: [Tutor] Sending sensible e-mail

2013-11-22 Thread Mark Lawrence

On 22/11/2013 10:19, Dominik George wrote:

Hi,


Subject: [Tutor] (no subject)


On a side note, please learn how to send e-mail.

Thanks,
Nik



At least Steven D'Aprano and myself love guessing games, let's have some 
occasionally please :)


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: [Tutor] Two subsequent for loops in one function

2013-11-22 Thread Don Jennings

On Nov 22, 2013, at 9:24 AM, Rafael Knuth wrote:

> Hej there,
> 
> newbie question: I struggle to understand what exactly those two
> subsequent for loops in the program below do (Python 3.3.0):
> 
> for x in range(2, 10):
>for y in range(2, x):
>if x % y == 0:
>print(x, "equals", y, "*", x//y)
>break
>else:
>print(x, "is a prime number")

Let's step through the code. The outer for loop will iterate over the values of 
range(2, 10):

>>> range(2, 10)
[2, 3, 4, 5, 6, 7, 8, 9]

So, each time the loop executes, x will be one of the values in that list. The 
inner loop then checks to see if any values up to but not including that value 
are evenly divisible by it. Let's choose 5 to see what will happen during that 
loop. The inner loop will then iterate over the values of range(2, 5):

>>> range(2, 5)
[2, 3, 4]

So, here is what happens during the x % y:

>>> 5 % 2
1
>>> 5 % 3
2
>>> 5 % 4
1

It is never equal to 0; the print(x, "is a prime number") will execute. Perhaps 
it's the "else" clause which is confusing? From the tutorial [1], I quote:

When used with a loop, the else clause has more in common with the else clause 
of a try statement than it does that of if statements: a try statement’s else 
clause runs when no exception occurs, and **a loop’s else clause runs when no 
break occurs**. (emphasis mine)

Take care,
Don

[1] http://docs.python.org/3/tutorial/controlflow.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a package to "un-mangle" characters?

2013-11-22 Thread Steven D'Aprano
On Thu, Nov 21, 2013 at 12:04:19PM -0800, Albert-Jan Roskam wrote:
> Hi,
> 
> Today I had a csv file in utf-8 encoding, but part of the accented 
> characters were mangled. The data were scraped from a website and it 
> turned out that at least some of the data were mangled on the website 
> already. Bits of the text were actually cp1252 (or cp850), I think, 
> even though the webpage was in utf-8 Is there any package that helps 
> to correct such issues?

Python has superpowers :-)

http://blog.luminoso.com/2012/08/20/fix-unicode-mistakes-with-python/



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


Re: [Tutor] Two subsequent for loops in one function

2013-11-22 Thread Danny Yoo
I agree with Peter Otten.  I want to try restating what he said to try to
emphasize what I think is the key point.


One basic skill that you learn as a programmer is how to handle nesting.
 One strategy is to give things names.  This can have benefits:

  1.  The name itself might make the code easier to read.
  2.  The thing being named might be simpler to understand in isolation to
the whole.


So when we look at the original program here here:

> for x in range(2, 10):
> for y in range(2, x):
> if x % y == 0:
> print(x, "equals", y, "*", x//y)
> break
> else:
> print(x, "is a prime number")

we can start to tease it apart, by giving names to sections of the program.
 Imagine a box or contour being drawn around the inner loop:


for x in range(2, 10):
+--
 |for y in range(2, x):
 |   if x % y == 0:
 |   print(x, "equals", y, "*", x//y)
 |   break
 |else:
 |   print(x, "is a prime number")
 --

We can give that thing a name!  We can do this naming with functions.
 Here's one approach: let's take that inner loop and make it into its own
function.

#
def InnerLoop(x):
for y in range(2, x):
if x % y == 0:
print(x, "equals", y, "*", x//y)
break
else:
print(x, "is a prime number")

for x in range(2, 10):
InnerLoop(x)
#

This is fairly mechanical: see what variables are "free" in the body of the
thing we're pulling out and make them arguments to the function.  Here, the
inner loop depends on the value of x, so that's why it becomes an argument
in the function we've named "InnerLoop".


We can do this extract-and-name process again and again: good taste tells
us where we can take things too far and make the resulting code look silly.
 For example, we might do the same to the "x % y == 0" part of the inner
loop's computation.  This might look something like this:

#
def IsDivisibleBy(x, y):
"""Returns true if y divides evenly into x."""
return x % y == 0

def InnerLoop(x):
for y in range(2, x):
if IsDivisibleBy(x, y):
print(x, "equals", y, "*", x//y)
break
else:
print(x, "is a prime number")

for x in range(2, 10):
InnerLoop(x)
#

This might be taking naming too far.  Some people can see "x % y == 0" and
know that this is essentially a divisibility test.


But hopefully the point is clear: if some piece of code is complex, you
might be able to take a reductionist approach, pull it apart, and give
names so you can look at the loops in isolation, rather than all together.
 Reductionism doesn't always work in all contexts: sometimes we can chop up
the code so much that the result don't hold together.  Figuring out where
that balance is takes experience.  I'm still trying to learn the right
balance myself.  :P


Now, by the way, "InnerLoop" is an absolutely horrible name.  Can you take
a look at that function in isolation and describe what it is doing?  If so,
you can rename InnerLoop to something more appropriate.  That's what I
think Peter's point is in his naming of the inner loop to "unknown".  Don't
leave it named that way.  Once you figure out what it's doing, give it a
good name.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Issue w/ while loops

2013-11-22 Thread Danny Yoo
> Ok, got you!
>
> print("TIME TRACKING")
>
> while True:
> hours_worked = input("How many hours did you work today? ")
> try:
> hours_worked = float(hours_worked)
> break
> except ValueError:
> print ("Invalid input")
> if hours_worked < 24:
> print("You must be a human.")
> else:
> print("You must be a cyborg.")
>

Here's an example where naming the while loop might be helpful in making
the code easier to understand.  Or not.  :P  Let's see what this might look
like.  First, let's take the while loop and make it a function:


#
def GiveMeAGoodName():
while True:
hours_worked = input("How many hours did you work today? ")
try:
hours_worked = float(hours_worked)
break
except ValueError:
print ("Invalid input")
return hours_worked

print("TIME TRACKING")
hours_worked = GiveMeAGoodName()
if hours_worked < 24:
print("You must be a human.")
else:
print("You must be a cyborg.")
#

Here, the function-extracting is a little more complex, because there's an
implicit passing of data from one part of the program to the other.  The
loop continues to run till hours_worked is a good float, after which the
rest of the program uses that float.  So that's why the "GiveMeAGoodName"
returns something.


We can look at GiveMeAGoodName(): it's tryingt to get the number of hours
worked.  Let's call it "AskForHoursWorked".


###
def AskForHoursWorked():
while True:
hours_worked = input("How many hours did you work today? ")
try:
hours_worked = float(hours_worked)
break
except ValueError:
print ("Invalid input")
return hours_worked

print("TIME TRACKING")
hours_worked = AskForHoursWorked()
if hours_worked < 24:
print("You must be a human.")
else:
print("You must be a cyborg.")
#


If we have a better feeling for how control flow interacts with functions,
we might simplify the lines in AskForHoursWorked() a little bit.  Here's
one restatement of that function that does the same thing:

#
def AskForHoursWorked():
while True:
hours_worked = input("How many hours did you work today? ")
try:
return float(hours_worked)
except ValueError:
print ("Invalid input")
#

I'd argue that this is a little clearer because, in this variation,
hours_worked is now definitely just a string throughout the program's run.
 It doesn't waffle between being a string and being a number.  The function
itself is a little shorter because we can do a "return" to get out of the
function, rather than do the "assign the value, break, then return" that we
had in the original code.


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


[Tutor] raw input program is still running.

2013-11-22 Thread Imrose Baga
Hi I've just started using python. I tried to use raw input for name, city
and state. But only my name shows up and then when i try to close the
program, it is showed as still running and asks if I wish to kill the
program. please any help
-- 
Regards,

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


[Tutor] Extarcting data tables from a text file

2013-11-22 Thread Ruben Guerrero
Dear tutor,

I am  a beginner in python and I need  your guidance to  write a python
script  to extract many nxn data tables of variable nunber of rows from a
text file as in the following example

  Condensed to atoms (all electrons):
  1  2  3  4  5  6
 1  Cl   0.00   0.304108  -0.101110  -0.108502  -0.108502   0.024111
 2  C0.304108   0.00   0.515965   0.332621   0.332621  -0.004054
 3  C   -0.101110   0.515965   0.00  -0.013334  -0.013334   0.352916
 4  H   -0.108502   0.332621  -0.013334   0.00  -0.133436  -0.028924
 5  H   -0.108502   0.332621  -0.013334  -0.133436   0.00  -0.028924
 6  H0.024111  -0.004054   0.352916  -0.028924  -0.028924   0.00
 7  H   -0.030910  -0.074027   0.364085  -0.053300   0.048704  -0.123402
 8  H   -0.030910  -0.074027   0.364085   0.048704  -0.053300  -0.123402
  7  8
 1  Cl  -0.030910  -0.030910
 2  C   -0.074027  -0.074027
 3  C0.364085   0.364085
 4  H   -0.053300   0.048704
 5  H0.048704  -0.053300
 6  H   -0.123402  -0.123402
 7  H0.00  -0.118520
 8  H   -0.118520   0.00
 Mulliken atomic charges:

to other text  file with the numerical information in the table
 concatenated by columns. The phrases in red always delimite the tables in
the original file. In the python generated file I need the following:   a
text flag (maybe  a line with the "O" charanter) delimiting each table, a
second line with the total number of rows (atoms) in the table followed by
a line with the ordered string of chemical symbols separated by a silgle
space.

My aim is load the numerical data from this file  to a c++ program to
process this information.

Thanks in advance.

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


Re: [Tutor] raw input program is still running.

2013-11-22 Thread Amit Saha
On Sat, Nov 23, 2013 at 7:02 AM, Imrose Baga  wrote:
> Hi I've just started using python. I tried to use raw input for name, city
> and state. But only my name shows up and then when i try to close the
> program, it is showed as still running and asks if I wish to kill the
> program. please any help

Please share your program.



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


Re: [Tutor] raw input program is still running.

2013-11-22 Thread Steven D'Aprano
On Fri, Nov 22, 2013 at 01:02:13PM -0800, Imrose Baga wrote:
> Hi I've just started using python. I tried to use raw input for name, city
> and state. But only my name shows up and then when i try to close the
> program, it is showed as still running and asks if I wish to kill the
> program. please any help

Just a moment, let me get my crystal ball and see what you are doing...

I see... nothing. Perhaps my crystal ball is out of order? Next time, 
please help us to help you by showing the code you are running, since 
crystal balls are often inaccurate, and while inspecting the entrails of 
animals is always correct, it does tend to me smelly and messy.


You can interrupt whatever Python is doing this way:

* click on the window that is running Python

* hold down the Control key

* press the C key.

You may need to do that a couple of times.

Or, you can just close the window and kill the program, that will be 
harmless.



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


Re: [Tutor] Extarcting data tables from a text file

2013-11-22 Thread Mark Lawrence

On 22/11/2013 16:25, Ruben Guerrero wrote:

Dear tutor,

I am  a beginner in python and I need  your guidance to  write a python
script  to extract many nxn data tables of variable nunber of rows from
a text file as in the following example

Condensed to atoms (all electrons):
   1  2  3  4  5  6
  1  Cl   0.00   0.304108  -0.101110  -0.108502  -0.108502
0.024111
  2  C0.304108   0.00   0.515965   0.332621   0.332621
  -0.004054
  3  C   -0.101110   0.515965   0.00  -0.013334  -0.013334
0.352916
  4  H   -0.108502   0.332621  -0.013334   0.00  -0.133436
  -0.028924
  5  H   -0.108502   0.332621  -0.013334  -0.133436   0.00
  -0.028924
  6  H0.024111  -0.004054   0.352916  -0.028924  -0.028924
0.00
  7  H   -0.030910  -0.074027   0.364085  -0.053300   0.048704
  -0.123402
  8  H   -0.030910  -0.074027   0.364085   0.048704  -0.053300
  -0.123402
   7  8
  1  Cl  -0.030910  -0.030910
  2  C   -0.074027  -0.074027
  3  C0.364085   0.364085
  4  H   -0.053300   0.048704
  5  H0.048704  -0.053300
  6  H   -0.123402  -0.123402
  7  H0.00  -0.118520
  8  H   -0.118520   0.00
Mulliken atomic charges:

to other text  file with the numerical information in the table
  concatenated by columns. The phrases in red always delimite the tables
in the original file. In the python generated file I need the following:
   a text flag (maybe  a line with the "O" charanter) delimiting each
table, a second line with the total number of rows (atoms) in the table
followed by a line with the ordered string of chemical symbols separated
by a silgle space.

My aim is load the numerical data from this file  to a c++ program to
process this information.

Thanks in advance.

-Ruben.



I'm sorry but we don't write code for you here.  I suggest that you 
start out by reading the tutorial here 
http://docs.python.org/3/tutorial/index.html, try writing something and 
when and if you run into problems please feel free to get back to us.


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


[Tutor] Using tkinter::ttk::treeview to implement data view.

2013-11-22 Thread Davnobezimeni Sasha Balagura
Hello.
I have to implement parcer for .txt file with special formating. There are
a lot of data to display, modify, add. I decided to use
tkinter::ttk:treeviev for this. And it really good one, very fast and good
looking..but..
But I cant set borders between cells, like in Excel. Is it real at all for
this widget? Or i've just overlooked something?

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


[Tutor] numrows returning -1

2013-11-22 Thread Paul Steele
Hey all...

I am getting a numrows count of -1 when I print numrows (see line 23 of the
code).   What does "-1" mean?I think my connection is working and my
table has data.

Here's my code...

#!/usr/bin/python
# -*- coding: utf-8 -*-

import mysql.connector
from mysql.connector import errorcode

try:
  con = mysql.connector.connect(user='root', password='pw848596',
  host='127.0.0.1',
  database='mydb')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exists")
  else:
print(err)
else:
  cur=con.cursor()

  cur.execute("select Name from Rosters")
  numrows = cur.rowcount
  print numrows
  for x in xrange(0,numrows):
  row = cursor.fetchone()
  print "Name"

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


Re: [Tutor] Using tkinter::ttk::treeview to implement data view.

2013-11-22 Thread Alan Gauld

On 23/11/13 01:19, Davnobezimeni Sasha Balagura wrote:

I have to implement parcer for .txt file with special formating. There
are a lot of data to display, modify, add. I decided to use
tkinter::ttk:treeviev for this. And it really good one, very fast and
good looking..but..
But I cant set borders between cells, like in Excel. Is it real at all
for this widget? Or i've just overlooked something?


There has been very little discussion of the ttk widgets on the tutor 
list so I'm not sure that many folks here use them (I know I've been 
meaning to play for a while). You might get a better response on the 
Tkinter mailing list (also available on gmane.org as 
gmane.comp.python.tkinter)


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] numrows returning -1

2013-11-22 Thread Alan Gauld

On 22/11/13 22:08, Paul Steele wrote:


I am getting a numrows count of -1 when I print numrows (see line 23 of
the code).   What does "-1" mean?I think my connection is working
and my table has data.


I don't know about MySQL but the sqlite3 module documentation says:

---
As required by the Python DB API Spec, the rowcount attribute “is -1 in 
case no executeXX() has been performed on the cursor or the rowcount of 
the last operation is not determinable by the interface”. This includes 
SELECT statements because we cannot determine the number of rows a query 
produced until all rows were fetched.



That may help but you should check the docs for the mysql module for 
details of that implementation.


Also I notice something odd in your code:

  cur.execute("select Name from Rosters")
  numrows = cur.rowcount
  print numrows
  for x in xrange(0,numrows):
  row = cursor.fetchone()  #AG - You never use row?
  print "Name" #AG - just prints the literal string


You do realize that this will not print the names retrieved? It will 
print the literal string "Name" once for each row found. I suspect 
that's not what you want?


Check the examples on the sqlite3 module page for examples
(in sqlite) of how to do what (I think) you want.



--
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] (no subject)

2013-11-22 Thread 久場海人
Hi. I began programming literally 2 days ago.

This is a code to setup password and confirms to make sure they both match,
and I need to know what coding I can use to loop back to specific line to
start the code over if the user were to incorrectly typed in the password.


1. CreatePassword = raw_input ("Create New Password: ")
2. ConfirmationPass = raw_input ("Retype Password: ")
3.
4.
5. if CreatePassword == ConfirmationPass:
6. print ("Password Set!")
7. else:
8. print ("Password did not match!")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Reading number x and printing number x+1

2013-11-22 Thread G. McKinnon Ryall
I have a script that outputs results to a file (one file, reused.) I would
like to have an output file in this format

#
(blank line)
(output from program (only one line))
name
(T/F)
(result iteration, shortened to x.)
#-
so like this
#-

55
Joe
false
1

96
Bob
true
2

28
Mike
true
3
#-

I couldn't think of a way to read the last written number (it would be a
multiple of 5). I know how to add 1 and print. Also, how would I arrange
#
output.write("\n")
output.write(x_string)
output.write("\n")
output.write(name)
output.write("\n")
output.write(exists_string)
output.write("\n")
#---
so that it didn't show up like this
FIRST TIME

101
nope
True
1
SECOND TIME

101
nope
True
1
4554073
yup
True



Also, how would I make this program better in general?



#BEGIN
# Importing exists. Simple English. Nonsensical, but English nonetheless.
from os.path import exists
#Asking for your name
name = raw_input("Hello!\nWhat is your name?\n\n")
#Asking for the number and reacting to given response
x = int(raw_input("\tFor the purposes of testing, please select a number
between one and 100\n\n\t"))

if x < 0:
x = 4554073
print "\t\tNo negatives, asshole. Number changed to '4554073' for
'ASSHOLE', asshole."
elif x == 0:
print "\t\tMore than zero."
elif x > 100:
x = 101
print "\t\tDon't fuck with the computer."
elif x == 42:
print "\t\tThe meaning of life, the universe and everything."
elif x == 7:
print "\t\t7. How... creative. *cough* UNORIGINAL *cough*"
elif x == 3:
print "\t\t3! It's a magic numba\'. Yes it is! It's a magic numba\'."
elif x == 37:
print "\t\tThe two most common numbers."
elif x == 99:
print "\t\tI got 99 problems and a- wait, no, that's your IQ."
elif x == 27:
print "\t\tCONGRATULATATIONS! YOU'VE FOUND MY FOAVORITE NOMBER!"
else:
print "In all aspects your number appears to be normal."
#Changing name to a string
name_string = "%s" % name
#Changing x to a string
x_string = "%s" % x
#Checking if file exists for archival purposes
exists = exists("number_output.txt")
exists_string = "%s" % exists
#Opening output file
number_output = 'number_output.txt'
output = open(number_output, 'a')
#Writing to file
output.write("\n")
output.write(x_string)
output.write("\n")
output.write(name)
output.write("\n")
output.write(exists_string)
output.write("\n")
#END
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor