现在我正在努力在屏幕上制作Python“类型”(我感到无聊和实验性),而且我自己得到了这么多,但我找不到任何方法在我的脚本中使用小数因为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()
有什么想法吗?
编辑:不要告诉我如何使这更简单,因为这只是为了学习。
答案 0 :(得分:5)
为什么不使用随机均匀分布?在这种情况下,您可以获得非整数值。使用:
import random
random.uniform(0.1,0.4)
答案 1 :(得分:1)
如果您 使用randint,则可以执行time_num = randint(1, 4)/10
。
答案 2 :(得分:-1)
for i in range(0, 587):
......没有任何东西在那里缩进(除非你没有在问题中发布缩进)..所以它实际上无用
对于time_num
变量,您可以像这样投射它:
time_num = double(random.randrange(1, 4) \ 10
你必须尝试在milliseconds
中获取它,因为python使用Second time import
但您也可以改为使用randint
使用random.randrange(#1, #2)
我不知道sys import
但您使用了这个 - &gt; sys.stdout.write(str(scripti));
但是您的变量名为"script"
.........