Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
import sys
import socket
import time
ip=([(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])


x="........"

def print_word_slowly(word):
    for i in word:
        sys.stdout.write(i)
        sys.stdout.flush()
        time.sleep(0.2)
        
        
        
print "Loading...\n"
print print_word_slowly("obtaining IP address......."), "     \nIP Address obtained. IP Address is %s."%(ip)



This prints out:
Loading...                                                                                                                               
                                                                                                                                         
obtaining IP address.......None                                                                                                          
IP Address obtained. IP Address is 172.17.29.xxx.

How can I get rid of 'None' after 'obtaining IP address.....'?
Posted
Updated 9-Jun-18 6:25am
v2
Comments
Sergey Alexandrovich Kryukov 10-Mar-15 23:53pm    
In Python, None gets rid of you. :-)
—SA
Blake Allen 11-Mar-15 0:05am    
AAAGH how does None get rid of me!!?
Blake Allen 11-Mar-15 0:06am    
Good thing I'm trying to get rid of it, I don't wanna die

The issue is down to the hidden return value from a Python function. If a function does not include a return statement, then it automatically adds one, and returns the value None, as described in https://docs.python.org/3.3/reference/simple_stmts.html#index-21[^]. So your print statement calls the function, which prints the message, then gets the return value of None and prints that, before printing the remainder of its parameters.
 
Share this answer
 
You should delete the first "Print" before your "print_word_slowly() function".
Try call your function directly rather than "Print" it.
 
Share this answer
 
Comments
Dave Kreskowiak 9-Jun-18 12:37pm    
You're only THREE YEARS too late.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900