简体   繁体   English

如何在导入 numpy 模块时获取用户输入“n = str(input("Give str"))”?

[英]How do I take user input "n = str(input("Give str"))" while having the numpy module imported?

my problem is that I can't take input from user ("n = str(input("Give str"))") while having the numpy module imported.我的问题是在导入 numpy 模块时我无法从用户 ("n = str(input("Give str"))") 获取输入。 I'm new to basically everything in Python and can't get past this problem.我对 Python 中的所有内容基本上都是新手,无法解决这个问题。

No matter what I do,不管我做什么,

"TypeError: 'numpy.ndarray' object is not callable".

The borrowed code I am working with:我正在使用的借用代码:

import pyaudio
import numpy as np

noteslist = {"C":-10,"C#":-9,"D":-8,"D#":-7,"E":-6,"E#":-5,"F":-4,"F#":-3,"G":-2,"G#":-1,"A":0,"A#":1,"B":2}

n = input("Give") 

fs = 44100       
duration = 5.0  
f = 432.0        

#Until this point I need to be albe to code as if no modules (numpy) have been imported.
#Part 2 of the code

input = np.sin(2*np.pi*np.arange(fs*duration)*f/fs)

def play(input):
    sample = (input).astype(np.float32)
    volume = 0.25
    p = pyaudio.PyAudio()
    stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True)
    stream.write(volume*sample)
    stream.stop_stream()
    stream.close()
    p.terminate()

play(input)

Ideas on how to overcome this problem although I have no idea how to realise them:关于如何克服这个问题的想法虽然我不知道如何实现它们:

  1. Make the code create another file or something and paste second half of the code, since the second half is only responsible for playing generated sound (sin).让代码创建另一个文件或其他东西并粘贴代码的后半部分,因为后半部分只负责播放生成的声音(sin)。
  2. Split the code into some kind of sections: one without numpy imported, another with numpy imported.将代码拆分为某种部分:一个没有导入 numpy,另一个导入了 numpy。
  3. Make the numpy import in the second half of the code, like time.sleep -> import numpy在代码的后半部分进行 numpy 导入,例如 time.sleep -> import numpy

Thanks so much!非常感谢!

input is a built-in function in python. input是python中的内置函数。
Reference: https://docs.python.org/3/library/functions.html#input参考: https ://docs.python.org/3/library/functions.html#input

You are currently using it as a variable for your numpy array.您当前正在将它用作numpy数组的变量。
This will not work.这行不通。

Instead rename it to another variable.而是将其重命名为另一个变量。

Consider this updated code.考虑这个更新的代码。

import pyaudio
import numpy as np

noteslist = {
    "C": -10,
    "C#": -9,
    "D": -8,
    "D#": -7,
    "E": -6,
    "E#": -5,
    "F": -4,
    "F#": -3,
    "G": -2,
    "G#": -1,
    "A": 0,
    "A#": 1,
    "B": 2,
}

n = input("Give")

fs = 44100
duration = 5.0
f = 432.0

A = np.sin(2 * np.pi * np.arange(fs * duration) * f / fs)

def play(A):
    sample = (A).astype(np.float32)
    volume = 0.25
    p = pyaudio.PyAudio()
    stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True)
    stream.write(volume * sample)
    stream.stop_stream()
    stream.close()
    p.terminate()


play(A)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM