i get different answers based on run platform

L

linda

I have this simple palindrome program that yields different results
depending on whether I run it from Windows or from IDLE. The answer
is correct off IDLE, but why is this the case? Here's the code:

def reverse(text):
return text[::-1]
def is_palindrome(text):
return text==reverse(text)
while True:
something=input('enter text:')
print(something)
print(something[::-1])
if (is_palindrome(something)):
print("yes, it is a palindrome")
break
else:
print("no, it is not a palindrome")
continue
print ('done')

Thanks for your help.
 
J

John Gordon

In said:
I have this simple palindrome program that yields different results
depending on whether I run it from Windows or from IDLE. The answer
is correct off IDLE, but why is this the case? Here's the code:

Your code contains debugging statements that print the value of the normal
string and the reversed string. What do those statements print when you
run your program under Windows? That should go a long way towards telling
you what the problem is.

(Perhaps Windows leaves a linefeed character hanging at the end of the
input line, which upsets the palindromic balance?)

By the way, I could not make your program work as you provided it; I had
to replace input() with raw_input(). Does it really work for you this way?
 
M

MRAB

I have this simple palindrome program that yields different results
depending on whether I run it from Windows or from IDLE. The answer
is correct off IDLE, but why is this the case? Here's the code:

def reverse(text):
return text[::-1]
def is_palindrome(text):
return text==reverse(text)
while True:
something=input('enter text:')
print(something)
print(something[::-1])
if (is_palindrome(something)):
print("yes, it is a palindrome")
break
else:
print("no, it is not a palindrome")
continue
print ('done')

Thanks for your help.
Try printing ascii(something) too.

There's a known bug in Python 3.2 where it leaves a training '\r', if
that's what you're using (http://bugs.python.org/issue12435), fixed in
Python 3.2.1.
 
E

Ethan Furman

linda said:
I have this simple palindrome program that yields different results
depending on whether I run it from Windows or from IDLE. The answer
is correct off IDLE, but why is this the case? Here's the code:

def reverse(text):
return text[::-1]
def is_palindrome(text):
return text==reverse(text)
while True:
something=input('enter text:')

try changing this to:
something = input('enter text:').rstrip('\n')
print(something)
print(something[::-1])
if (is_palindrome(something)):
print("yes, it is a palindrome")
break
else:
print("no, it is not a palindrome")
continue
print ('done')
 
M

MRAB

Your code contains debugging statements that print the value of the normal
string and the reversed string. What do those statements print when you
run your program under Windows? That should go a long way towards telling
you what the problem is.

(Perhaps Windows leaves a linefeed character hanging at the end of the
input line, which upsets the palindromic balance?)

By the way, I could not make your program work as you provided it; I had
to replace input() with raw_input(). Does it really work for you this way?
It's Python 3.

Python 2's raw_input() has been renamed input() in Python 3 and Python
2's input() was never in Python 3 because it uses eval on the string,
which usually undesirable.
 
E

Ethan Furman

John said:
By the way, I could not make your program work as you provided it; I had
to replace input() with raw_input(). Does it really work for you this way?

input() is the 3.x name for raw_input()

~Ethan~
 
L

linda

I tried something = input('enter text:').rstrip('\n') as suggested but
the problem persists. BTW, the intermediate print commands agree and
so are not an issue. The disagreement is in IDLE correctly
identifying palindromes and Windows not. I do suspect it may be a
trailing '\r' issue. Is there an easy fix or shall I wait for the new
Python versions to be released? Thanks for helping.
 
E

Ethan Furman

linda said:
I tried something = input('enter text:').rstrip('\n') as suggested but
the problem persists. BTW, the intermediate print commands agree and
so are not an issue. The disagreement is in IDLE correctly
identifying palindromes and Windows not. I do suspect it may be a
trailing '\r' issue. Is there an easy fix or shall I wait for the new
Python versions to be released? Thanks for helping.

My apologies -- change the '\n' to '\r' and that will hopefully do the
trick.

~Ethan~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top