现在,我正在您的屏幕上制作Python "type“(我感到无聊和实验性),而且我自己已经做了这么多,但是我找不到在脚本中使用小数的任何方法,因为Python只接受randint()的整组数字。
#include <stdio.h>;
import sys;
import time;
import os;
import random;
os.system("clear");
script = "I just really need to type something right now. I'm not sure why, but I really feel like typing on the Chromebook right now. It seems kinda weird, but I like doing this. It helps me practice what I'm doing. I've noticed that I'm a lot worse at typing lately, and I don't know why. Maybe it's because I've been working with a chunky keyboard, and these Chromebook keyboards are extremely thin. Yup, this is actually entertaining for me. I'm not exactly sure why I want to do this, but I just do. It's almost addicting to me, and I don't know why.\n";
for i in range(0, 587):
sys.stdout.write(str(scripti));
sys.stdout.flush();
time_num = random.randint(0.1, 0.4);
time.sleep(time_num);我得到的输出是:
ITraceback (most recent call last):
File "main.py", line 11, in <module>
time_num = random.randint(0.1, 0.4);
File "/usr/lib64/python2.7/random.py", line 242, in randint
return self.randrange(a, b+1)
File "/usr/lib64/python2.7/random.py", line 187, in randrange
raise ValueError, "non-integer arg 1 for randrange()"
ValueError: non-integer arg 1 for randrange()有什么想法吗?
编辑:不要告诉我如何使这更简单,因为这只是为了学习。
发布于 2015-03-28 06:07:00
你为什么不用随机均匀分布代替?在这种情况下,您可以获得非整数值。使用:
import random
random.uniform(0.1,0.4)https://stackoverflow.com/questions/29314213
复制相似问题