我一直在这里使用基于python公式的基本计算器

2024-07-05 14:34:10 发布

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

def answers():
    ppv = tp/(tp + fp)
    rcl = tp/(tp + fn)
    return ppv, rcl


print("enter the value of true positive")
tp = input()
print("enter the value of false negative")
fn = input()
print("enter the value of false positive")
fp = input()
print("enter the value of true negative")
tn = input()
print('ppv and recall answers')
print(answers)

这是一个基于python中公式的基本计算器,它不显示任何错误,但也不显示所需的输出。有关更多信息,请查看下图

here check the output it returns something like this <function answers at 0x000002377567F040>


Tags: ofthefalsetrueinputvalueanswersfn
3条回答

您正在打印函数而不是调用它

print(answers())

而不是

print(answers)

将参数传递给函数,而不是全局设置和访问参数,也更为简洁

见下文(类似于此):

  • 从用户那里获取输入
  • 调用函数并传递参数
def answers(tp, fp, fn):
     ppv = tp / (tp + fp)
     rcl = tp / (tp + fn)
     return ppv, rcl
    
    
print("enter the value of true positive")
_tp = input()
print("enter the value of false negative")
_fn = input()
print("enter the value of false positive")
_fp = input()

print('ppv and recall answers')
print(answers(int(_tp), int(_fp), int(_fn)))

您的代码中有几个错误。您应该学习一些python的基本教程,这些教程应该展示如何调用函数和处理用户输入

您不调用函数,应该将输入转换为ints:

def answers():
    ppv = tp/(tp + fp)
    rcl = tp/(tp + fn)
    return ppv, rcl


print("enter the value of true positive")
tp = int(input())
print("enter the value of false negative")
fn = int(input())
print("enter the value of false positive")
fp = int(input())
print("enter the value of true negative")
tn = int(input())
print('ppv and recall answers')
print(answers())

也许更好的版本是将数字转换为float并将其作为参数传递:

def answers(tp, fn, fp):
    ppv = tp/(tp + fp)
    rcl = tp/(tp + fn)
    return ppv, rcl


print("enter the value of true positive")
tp = float(input())
print("enter the value of false negative")
fn = float(input())
print("enter the value of false positive")
fp = float(input())
#print("enter the value of true negative")
#tn = float(input())
print('ppv and recall answers')
print(answers(tp, fn, fp))

注意,我已经注释掉了请求tn的位,因为它没有在answers()函数中使用

相关问题 更多 >