【发布时间】:2017-09-07 12:15:26
【问题描述】:
我的程序应该玩游戏。这是我遇到问题的加载屏幕。当我运行它时,time.sleep 表现得好像它正在休眠 0.1 秒,而不是我输入的小得多的数字。为什么是这样?有什么办法可以缩短延误时间吗?代码如下:
import os
import random
import time
import sys
def mywrite(line2,line1,t,s,x='small'):
if x=='small':
x=0.0000000000000000000000000000000000000000000000000000000000000000000000001
else:
x=random.random()*t+s
word=''
for c in line1:
if line1.index(c)<len(line1)-1:
print(line2)
word=word+c
print(word)
time.sleep(x)
os.system('cls')
else:
print(line2)
word=word+c
print(word,' \n')
time.sleep(x)
mywrite('__________________________________________________________\n',' %33s'%'Scrambled',0.005,0.1,'random')
print(' Press "a" to play %30s'%'Press "s" to exit')
print('__________________________________________________________')
start=input()
if start=='a':
permission=1
if start=='s':
permission=0
if permission==0:
sys.exit()
if permission==1:
print("Choose Difficulty")
print('Easy -Press a')
print('Hard -Press b')
print('Insane -Press c')
diff=input()
y=0
while permission==1:
os.system('cls')
mywrite('''
_ _ _
(_) ___ __ _ __| | | | _ __ __ _
| | / _ \\ / _` | / _` | | | | '_ \\ / _` |
| | | (_) | | (_| | | (_| | | | | | | | | (_| |
|_| \\___/ \\__, | \\__,_| |_| |_| |_| \\__,_|
|___/
''', 0.005, 0.001)
time.sleep(2)
os.system('cls')
mywrite('''
_ _
(_) __ _ | | __ _
| | / _` | | | / _` |
| | | (_| | | | | (_| |
|_| \__, | |_| \__,_|
|___/
_
___ __| | _ __
/ _ \ / _` | | '_ \
| (_) | | (_| | | | | |
\___/ \__,_| |_| |_|
''', 0.005, 0.001)
time.sleep(2)
os.system('cls')
mywrite('''
_ _
| | __ _ (_) __ _
| | / _` | | | / _` |
| | | (_| | | | | (_| |
|_| \__,_| |_| \__, |
|___/
_
___ __| | _ __
/ _ \ / _` | | '_ \
| (_) | | (_| | | | | |
\___/ \__,_| |_| |_|
''', 0.005, 0.001)
time.sleep(2)
os.system('cls')
mywrite('''
_ _ _
| | ___ __ _ __| | (_) _ __ __ _
| | / _ \ / _` | / _` | | | | '_ \ / _` |
| | | (_) | | (_| | | (_| | | | | | | | | (_| |
|_| \___/ \__,_| \__,_| |_| |_| |_| \__, |
|___/
''', 0.005, 0.001)
time.sleep(4)
os.system('cls')
if y==2:
break
如果太长,这里是包含问题的部分:
import os
import random
import time
import sys
def mywrite(line2,line1,t,s,x='small'):
if x=='small':
x=0.0000000000000000000000000000000000000000000000000000000000000000000000001
else:
x=random.random()*t+s
word=''
for c in line1:
if line1.index(c)<len(line1)-1:
print(line2)
word=word+c
print(word)
time.sleep(x)
os.system('cls')
else:
print(line2)
word=word+c
print(word,' \n')
time.sleep(x)
while permission==1:
os.system('cls')
mywrite('''
_ _ _
(_) ___ __ _ __| | | | _ __ __ _
| | / _ \\ / _` | / _` | | | | '_ \\ / _` |
| | | (_) | | (_| | | (_| | | | | | | | | (_| |
|_| \\___/ \\__, | \\__,_| |_| |_| |_| \\__,_|
|___/
''', 0.005, 0.001)
time.sleep(2)
os.system('cls')
mywrite('''
_ _
(_) __ _ | | __ _
| | / _` | | | / _` |
| | | (_| | | | | (_| |
|_| \__, | |_| \__,_|
|___/
_
___ __| | _ __
/ _ \ / _` | | '_ \
| (_) | | (_| | | | | |
\___/ \__,_| |_| |_|
''', 0.005, 0.001)
time.sleep(2)
os.system('cls')
mywrite('''
_ _
| | __ _ (_) __ _
| | / _` | | | / _` |
| | | (_| | | | | (_| |
|_| \__,_| |_| \__, |
|___/
_
___ __| | _ __
/ _ \ / _` | | '_ \
| (_) | | (_| | | | | |
\___/ \__,_| |_| |_|
''', 0.005, 0.001)
time.sleep(2)
os.system('cls')
mywrite('''
_ _ _
| | ___ __ _ __| | (_) _ __ __ _
| | / _ \ / _` | / _` | | | | '_ \ / _` |
| | | (_) | | (_| | | (_| | | | | | | | | (_| |
|_| \___/ \__,_| \__,_| |_| |_| |_| \__, |
|___/
''', 0.005, 0.001)
time.sleep(4)
os.system('cls')
if y==2:
break
顺便说一句,我才刚接触 python 几天,所以请保持解释简单。谢谢。
【问题讨论】:
-
你应该避免写 0.00000000000000000000000000000000000000000000000000000000000000000000000001 并了解E-notation
-
我知道。我只是对那个 0 感到沮丧并发送了垃圾邮件。XD
-
离题,但为什么第一个“加载”艺术退格被转义,而其余的却没有?如果您不需要转义,请不要放置转义。如果这样做,请将它们放在任何地方(或者最好使用原始字符串表示法
r"something\with\backslashes") -
当我在没有它们的情况下运行第一个时,它没有正确显示。我把它们放进去后,它们都工作得很好。我的老师提到了一些关于它的事情,因为退格键有时会导致问题。