My code is below. Try running it if possible to see the problem. But basically, after returning the word as backwards (cc2) it then displays none on a new line. Why? and how do I prevent this in the future?

print "Welcome to the string reverser and palindrome checker. \n The program will prompt you for input when it is required."
raw_input("Press any key to continue...")

def revstr(cc):
	xx=0;cc2="";ab=[];xx2=0
	while xx<len(cc):
		ab[xx:xx]=cc[-xx-1]
		xx=xx+1
	while xx2<len(ab):
		cc2=cc2+ab[xx2]
		xx2=xx2+1
	print cc2


print revstr(raw_input("Enter string to be reversed"))
raw_input("Press any key to quit"); exit()

Recommended Answers

All 10 Replies

It's because your function revstr prints the reversed string, but then returns None (because there is no return statement). Since your program contains print revstr(..., this None value returned is printed. What you should do is replace print by return in revstr.
Note: in the forum, use (code=python) instead of (code) :)

Thanks man, that helps alot :)
Just started learning python this morning, so learning the little quircks :)

yeah I noticed that script somewhere before, but I didn't understand it, and I wanted to make sure I understand everything I'm using.
I'll check out those pages though.
Thanks

Proof that [::-1] works for spelling string in reverse:

s1 = "racecar"
s2 = "kayak"

print s1, s1[::-1]
print s2, s2[::-1]

:)

it should be a unittest for python strings !

Oh yeah, like:

print 'racecar' == 'racecar'[::-1]  # True

:)

Spelling a string in reverse the old fashioned way is always a good exercise in any computer language.

Here is an example in C language:

// spell a text string forward and backward

#include <stdio.h>

int main(void)
{
  char text[80];
  int k;

  strcpy(text, "Spell this string forward then backward");
  printf("String = %s \n", text);

  // forward spelling puts a space between characters
  for(k = 0; k < strlen(text); k++)
    printf("%c ", text[k]);
  printf("\n\n");

  // now the reverse spelling
  for(k = strlen(text)-1; k >= 0; k--)
    printf("%c ", text[k]);
    
  getchar();  // wait
}

This would be its Python language counter part:

text = "Spell this string forward then backward"
print text

# as you spell forward the comma puts a space between characters
for c in text:
    print c,

print "\n"

# now the reverse spelling
for c in reversed(text):
    print c,

raw_input()  # wait

same in perl

#!/usr/bin/env perl
$text = "hello world";
$rev = reverse $text;
print $text, "\n", $rev, "\n";

Here is an example in C language:

// spell a text string forward and backward

#include <stdio.h>

int main(void)
{
  char text[80];
  int k;

  strcpy(text, "Spell this string forward then backward");
  printf("String = %s \n", text);

  // forward spelling puts a space between characters
  for(k = 0; k < strlen(text); k++)
    printf("%c ", text[k]);
  printf("\n\n");

  // now the reverse spelling
  for(k = strlen(text)-1; k >= 0; k--)
    printf("%c ", text[k]);
    
  getchar();  // wait
}

The standard C functions strlen() and strcpy() require the inclusion of the header file string.h. Furthermore there was not need to add the extra code: strcpy(text, "Spell this string forward then backward"); C is capable of doing that for you as: char text[] = "Spell this string forward then backward"; No need to declared as an array of 80.
Calling strlen(text) every time inside the loop is extra too. size_t len = strlen(text); before the loop is all that you need, then you use len in the first for loop, and len -1 in the reverse for loop. int main(void) { /* requires a return 0; on success */ }

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.