请教:python脚本得到“TypeError: a float is required”错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请教:python脚本得到“TypeError: a float is required”错误相关的知识,希望对你有一定的参考价值。

import math

def check_fermat(a,b,c,n):

if math.pow(a,n) + math.pow(b,n) == math.pow(c,n):

print "Holy smokes, Fermat was wrong!"

else:

print "No, that does'nt work."

a = raw_input("Please input a number which is greater than 2:\n")

int(a)

check_fermat(a,3,4,4)

错误信息:
File "D:/Python/checkfermattest.py", line 4, in check_fermat
if math.pow(a,n) + math.pow(b,n) == math.pow(c,n):
TypeError: a float is required

根据错误提示,要求第4行的值需要为浮点数。我们做一下数据类型转换就行了。

if float(math.pow(a,n)) + float(math.pow(b,n)) == float(math.pow(c,n)):

参考技术A a是字符串,没有抓换成数字
改成 a = int(a)本回答被提问者采纳

Python TypeError:reduce_noise() 得到了一个意外的关键字

【中文标题】Python TypeError:reduce_noise() 得到了一个意外的关键字【英文标题】:Python TypeError: reduce_noise() got an unexpected keyword 【发布时间】:2021-09-23 11:55:42 【问题描述】:

大家好,我正在尝试使用 python 进行音频分类,我安装了一个包,当我尝试使用这些函数时,它说“TypeError: TypeError: reduce_noise() got an unexpected keyword argument 'audio_clip' 听到代码功能。

导入 librosa 将 numpy 导入为 np 将降噪导入为 nr

def save_STFT(文件、名称、活动、主题): #读取音频数据 audio_data,sample_rate = librosa.load(文件) 打印(文件)

 #noise reduction
  noisy_part = audio_data[0:25000]
  reduced_noise = nr.reduce_noise(audio_clip=audio_data, noise_clip=noisy_part, verbose=False)

 #trimming
  trimmed, index = librosa.effects.trim(reduced_noise, top_db=20, frame_length=512, hop_length=64)

 #extract features
  stft = np.abs(librosa.stft(trimmed, n_fft=512, hop_length=256, win_length=512))
 # save features
  np.save("STFT_features/stft_257_1/" + subject + "_" + name[:-4] + "_" + activity + ".npy", stft)

这段代码在 conda 环境下的 jupyternote book 中运行,但它没有在 pycharm 中运行。 我在 PYcharm 中安装了 conda 环境,但它不起作用。你能帮我知道如何解决这个错误吗?

【问题讨论】:

【参考方案1】:

您的问题的答案在错误消息中。

"TypeError: TypeError: reduce_noise() got an unexpected keyword argument 'audio_clip' 

我猜你正在使用noisereduce Python 库。如果您查看文档,参数列表中没有audio_clip

正确代码示例:

reduced_noise = nr.reduce_noise(y=audio_data, y_noise=noisy_part, sr=SAMPLING_FREQUENCY) # check the SAMPLING_FREQUENCY

【讨论】:

谢谢。是的,我正在使用降噪库。我会试试这个。 @REDALPHA97 进展如何? 哦!对不起,谢谢。它奏效了。【参考方案2】:

您可能是在为较新版本的库引用旧 API 在较新版本的库中使用旧 API 的解决方法是

from noisereduce.noisereducev1 import reduce_noise

现在你可以重复使用你的代码了

reduced_noise = reduce_noise(audio_clip=audio_data, noise_clip=noisy_part, verbose=False)

【讨论】:

以上是关于请教:python脚本得到“TypeError: a float is required”错误的主要内容,如果未能解决你的问题,请参考以下文章

从 Python 运行 Matlab 脚本:TypeError: 'float' object is not iterable

请教如何在没有安装python的环境中执行py脚本

使用 python 运行 bash 脚本 - TypeError: bufsize must be an integer

我不明白为啥 Python 在此脚本中引发错误“TypeError: bad operand type for unary -: 'list''

python 新手请教,用java调用.py如何用java传入python参数,谢谢

TypeError: scatter() 得到了一个意外的关键字参数“trendline_options”(Plotly,Python)