[Tutor] Why does loop duplicate?

2015-12-16 Thread Ken Hammer
Intent is to print "Jack, Kack, " with "O" and "Q" delivering a longer 
suffix.  Instead, I get the printout shown with duplicates and a second 
deviation with "O" and "Q" as shown.
Why?  IDLE and Shell are Python 2.5.4 running on Windows 8.1.

prefixes = 'JKLMNOPQ'   ###FAILS WITH REPEATS
suffix = 'ack'
suffixb= 'uack'

for letter in prefixes:
if letter == "O":
print letter + suffixb
if letter == "Q":
print letter + suffixb
else:
print letter + suffix

>>> 
Jack
Jack
Kack
Kack
Lack
Lack
Mack
Mack
Nack
Nack
Ouack
Oack
Oack
Pack
Pack
Quack
Qack
>>> 

Thanks,  Ken Hammer

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


Re: [Tutor] Why does loop duplicate?

2015-12-16 Thread Mark Lawrence

On 16/12/2015 19:37, Ken Hammer wrote:

Intent is to print "Jack, Kack, " with "O" and "Q" delivering a longer suffix.  Instead, I get 
the printout shown with duplicates and a second deviation with "O" and "Q" as shown.
Why?


That's what you've told the code to do :)



prefixes = 'JKLMNOPQ'   ###FAILS WITH REPEATS


### OH NO IT DOESN'T - nothing personal but it's pantomine season in the 
UK :)



suffix = 'ack'
suffixb= 'uack'

for letter in prefixes:
 if letter == "O":
 print letter + suffixb
 if letter == "Q":
 print letter + suffixb
 else:
 print letter + suffix



You test for "O", but then follow with a test for "Q" and an else 
clause.  You could write :-


elif letter == "Q"

but the cleanest way of doing this is:-

for letter in prefixes:
if letter in ("O", "Q"):
print letter + suffixb
else:
print letter + suffix

--
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


Re: [Tutor] Why does loop duplicate?

2015-12-16 Thread Steven D'Aprano
On Wed, Dec 16, 2015 at 02:37:18PM -0500, Ken Hammer wrote:

> Intent is to print "Jack, Kack, " with "O" and "Q" delivering a 
> longer suffix.  Instead, I get the printout shown with duplicates and 
> a second deviation with "O" and "Q" as shown. Why?

Are you absolutely sure about that? When I run your code, I don't get 
any duplicates:


prefixes = 'JKLMNOPQ'   ###FAILS WITH REPEATS
suffix = 'ack'
suffixb= 'uack'
 
for letter in prefixes:
if letter == "O":
print letter + suffixb
if letter == "Q":
print letter + suffixb
else:
print letter + suffix

Output is:

Jack
Kack
Lack
Mack
Nack
Ouack
Oack
Pack
Quack


If you are getting duplicate lines, you must be running code that is 
different from the code you posted to us. We cannot help you with code 
we can't see.

As far as the code shown, look carefully at the code inside the 
for-loop:

if letter == "O":
print letter + suffixb
if letter == "Q":
print letter + suffixb
else:
print letter + suffix


The first test checks to see if the letter is "O", and if so, it prints 
"Ouack". Then, *regardless* of whether it just printed "Ouack" or not, 
it then does a second test, for "Q". Since Q is not O, it then prints 
"Oack".

No other letter gets treated that way.

Instead, you should change the second "if" to an "elif" (else if). Or 
better still:

if letter == "O" or letter == "Q":
print letter + suffixb
else:
print letter + suffix


which can be simplified even more to:

if letter in ("O", "Q"):
print letter + suffixb
else:
print letter + suffix



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


Re: [Tutor] interface

2015-12-16 Thread boB Stepp
On Mon, Dec 14, 2015 at 10:08 PM, Alex Kleider  wrote:
> Thank you, gentlemen (Alan, Ben, Mark,) for your advice.
> The consensus seems to be in favour of tkinter
> so I'll head in that direction.

If you are into books, "Programming Python, 4th ed." by Mark Lutz, has
an extensive section on tkinter (Alan had pointed this out to me, and
I have found Lutz's coverage very helpful).  Plus the book (very
thick!) has lots of other goodies.  Also, the now old book, "Python
and Tkinter Programming" by John E. Grayson is just about Tkinter
(with a capital "T") as it is copyrighted 2000.  Despite this I think
it is still quite useful even if you are working in Python 3 as, as
far as I can tell, t/Tkinter has not changed substantially in how the
coding goes.

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