Unwanted "None" in output

This is the place for queries that don't fit in any of the other categories.

Unwanted "None" in output

Postby rabowling » Tue Nov 05, 2013 8:41 pm

I have made it so all my text prints out letter-by-letter using this bit of code (I made sure to import sys before this code block):

Code: Select all
def delay_print(s):
    for c in s:
        sys.stdout.write('%s' % c)
        sys.stdout.flush()
        time.sleep(0.05)


I use raw_input() as a part of the text, and I have gotten it to print out letter-by-letter as well:

Code: Select all
q_one = raw_input(delay_print("Are you a boy, or a girl? "))


However, when I run the code, the letters print out one-by-one, like wanted, but I also get a "None" statement afterwards:

Code: Select all
Are you a boy, or a girl? None


I would like the text to still print out letter-by-letter, however I don't want the "None" at the end. This is only for my raw_input() commands. Any help?
rabowling
 
Posts: 4
Joined: Mon Nov 04, 2013 10:53 pm

Re: Unwanted "None" in output

Postby Yoriz » Tue Nov 05, 2013 8:45 pm

A function that doesn't return anything will return None by default.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 831
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Unwanted "None" in output

Postby rabowling » Tue Nov 05, 2013 10:16 pm

So is there a way to make it not return none?
rabowling
 
Posts: 4
Joined: Mon Nov 04, 2013 10:53 pm

Re: Unwanted "None" in output

Postby metulburr » Tue Nov 05, 2013 10:52 pm

normally people make functions to return something, and if they dont they return None by default. Using the functions in the manner you are however, you can just return an empty string to quiet the None.
Code: Select all
def delay_print(s):
    for c in s:
        sys.stdout.write('%s' % c)
        sys.stdout.flush()
        time.sleep(0.05)
    return ''
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Unwanted "None" in output

Postby ochichinyezaboombwa » Wed Nov 06, 2013 3:26 am

Code: Select all
return ''
is IMO a hack. I prefer a cleaner design. Don't have one function do two or more different things unnecessarily; instead, separate semantically and otherwise as much as possible: it's easier to write and read:

Code: Select all
def delay_print(s):
    for c in s:
        sys.stdout.write('%s' % c)
        sys.stdout.flush()
        time.sleep(0.05)

delay_print("Are you a boy, or a girl? ") # delay_print only does what it says it does
q_one = raw_input()# so does raw_input
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Unwanted "None" in output

Postby Kebap » Wed Nov 06, 2013 6:47 am

Can you keep the input cursor in the same line as the print though?
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Unwanted "None" in output

Postby ochichinyezaboombwa » Wed Nov 06, 2013 10:35 pm

Kebap wrote:Can you keep the input cursor in the same line as the print though?

Hmm?... Have you tried my code? It works like this:
Code: Select all
Are you a boy, or a girl? YES!
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests