Python:如何使用函数和循环显示/循环数组中的数字序列?视觉节拍器
好的,这是一个新的编码,我已经设法从Zed Shaw的PTHW、Google和一些现有的帮助中拼凑出来 我似乎不知道如何循环数组中的数字序列,一次只显示一个。这只是一个视觉上的东西。V2是需要帮助的。提前感谢您的帮助和解释 V1工作,但只在终端中及时打印“勾号”Python:如何使用函数和循环显示/循环数组中的数字序列?视觉节拍器,python,arrays,function,loops,Python,Arrays,Function,Loops,好的,这是一个新的编码,我已经设法从Zed Shaw的PTHW、Google和一些现有的帮助中拼凑出来 我似乎不知道如何循环数组中的数字序列,一次只显示一个。这只是一个视觉上的东西。V2是需要帮助的。提前感谢您的帮助和解释 V1工作,但只在终端中及时打印“勾号” #Accepts user's beats per minute and turns it into variable "x" x = int(raw_input("BPM:")) #Calculates floating point
#Accepts user's beats per minute and turns it into variable "x"
x = int(raw_input("BPM:"))
#Calculates floating point BPM instances in seconds
metspeed = 60.0 / x
#no idea
import threading
#function definition. No idea how, but it can print in desired time.
def visual():
threading.Timer(metspeed, visual).start()
print "tick"
#function call
visual()
V2根本不起作用
#2 This is my attempt at looping through the array. Doesn't work.
x = int(raw_input("BPM:"))
metspeed = 60.0 / x
#Number array. Better way if numbers go above 4?
i = [1, 2, 3, 4]
import threading
#attempt to pull from array and displaying only 1 number at a time.
#seems like a loop is needed, and a way to stop the loop without control-c
def visual():
threading.Timer(metspeed, visual).start()
print "%s\r" % i,
visual()
简单地使用时间睡眠怎么样
导入时间
x=int(原始输入(“BPM:”)
metspeed=60.0/x
i=[1,2,3,4]
对于i中的j:
打印j
时间。睡眠(metspeed)
如果要永远运行它(或直到用户使用Ctrl-C中断),请使用while
循环:
while True:
print "tick"
time.sleep(metspeed)
您可以使用
循环
在列表上循环
from itertools import cycle
from threading import Timer
counts = cycle(xrange(1, 5))
def visual():
print next(counts)
Timer(metspeed, visual).start()
visual()
这里有一种方法可以做到这一点
x = int(raw_input("BPM:"))
metspeed = 60.0 / x
import threading
import random
i = [1, 2, 3, 4]
def visual():
print random.choice(i)
mythread = threading.Timer(metspeed, visual)
mythread.start() ## start you thread process
myvar = False ## write a condition when it should stop
if myvar == False: ## check if the condition is met
mythread.cancel() ## stop the triggering of timer
我想它应该一直循环直到被打断。这将只经过一次循环。您是否尝试过使用while循环?您肯定也应该阅读更多关于计时器对象的内容:是的,就是这个!非常感谢。