python中的浮动

2024-06-02 23:42:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个节拍器。我的代码是:

import time
import sound

metronome = raw_input("")
int(metronome)

while 1==1:
  sound.play_effect('Drums_02')
  time.sleep(metronome)

当我运行这段代码时,它会出现一条错误消息,说“需要一个float” 如果有人能理解的话,我很伤心。你知道吗


Tags: 代码importinputplayrawtime错误sleep
3条回答

int(metronome)不将metronome转换为整数。它创建一个新的int,然后丢弃它,因为您不使用它。你想把它传给你的sleep电话:

time.sleep(int(metronome))

或者,如果您在多个位置使用它,请在第一位指定metronome一个int值:

metronome = int(raw_input(""))

metronome仍然是字符串,而不是整数。将int()的结果存储回变量:

metronome = int(mentronome)
int(metronome)

除了打印字符串的int表示之外,对变量不做任何操作。你知道吗

你想说:

metronome = int(metronome)

或者

metronome = int(raw_input(""))

相关问题 更多 >