Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

drichmond6021023

  • 2 years ago

This is a Doctor,Doctor it hurts when I do this question...but does anyone know why I get a "None" output when running from a module, and I don't get a "None" when I call a function from the Python Shell?

  • This Question is Open
  1. drichmond6021023
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    #As an exercise, write a function that takes a string as an #argument and outputs the letters backward, one per line. def spellBackwards(word): loopCount=-1 while -1*loopCount<=len(word): if word[loopCount]==None: return " " else: print (word[loopCount]) loopCount-=1 print (spellBackwards("backwards")) #I do get my backwards spelling, but at the end of that I get a "None". If I #comment out the print statement "None" does not print. What is going on #here? My if/else word[loopCount] apparently does nothing. def spellBackwards1(word): loopCount=1 while loopCount<=len(word): print (word[len(word)-loopCount]) loopCount+=1 print (spellBackwards1("backwards")) #this too gives me a "None" at the end. def spellFrontwards(word): loopCount=0 while loopCount<len(word): print (word[loopCount]) loopCount+=1 print (spellFrontwards("frontwards")) #but if the function is called from the shell I don't get the "None"

  2. msmithhnova
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    All functions return 'None' unless you use the keyword 'return to return something else. The shell suppresses the 'None' return. If you did some_var = spellFrontwards(word) and then print some_var it would show you the 'None'

  3. msmithhnova
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Instead of using print at the end you could just type spellFrontwards("frontwards") ad your function already does the printing or you could make the function build up a variable with what you want to print and return it, then your last print would work as expected.

  4. drichmond6021023
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank You msmithhnova

  5. rsmith6559
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Here's a recursive way to do it: def reverse( string ): if( len( string ) == 0 ): return char = string[ 0 ] reverse( string[ 1: ] ) print char if( __name__ == "__main__" ): print reverse( "Hello World" )

  6. drichmond6021023
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks rsmith6559, that nice little program will help me think about recursion.

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.