Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

python - Why does "none" come up when using time.sleep?

import time
import random

print "Wanna play a game?"

raw_input(),
time.sleep(1)
print ("Think of a number (1-5), I won't see, I promise. 
"),
    time.sleep(5)

print "You picked? Now write it down."
raw_input(),
time.sleep(3)

print ("Let me guess... 
"),
    time.sleep(2)

print ((random.randint(1, 5))), 
    time.sleep(20000)

When I run it in terminal, this happens:

Wanna play a game?

yes
Think of a number (1-5), I won't see, I promise. 
None
You picked? Now write it down.
Let me guess... 
None
1

Why does "none" pop up?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

The , in your code adding a new argument to the print statements and printing it, which in this case is the return value of time.sleep() (None)

Remove those, fix the indentation, and it works.

import time
import random

print "Wanna play a game?"

raw_input()
time.sleep(1)
print ("Think of a number (1-5), I won't see, I promise. 
")
time.sleep(5)

print "You picked? Now write it down."
raw_input()
time.sleep(3)

print ("Let me guess... 
")
time.sleep(2)

print ((random.randint(1, 5)))
time.sleep(20000)

Output:

Wanna play a game?
y
Think of a number (1-5), I won't see, I promise. 
You picked? Now write it down.
1
Let me guess... 
3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...