Python:返回可能不返回值 [英] Python: Return possibly not returning value

查看:65
本文介绍了Python:返回可能不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好 Stack Overflow 社区,

Hello Stack Overflow Community,

我正在尝试编写一个程序来检查费马大定理.我遇到的问题是出现错误提示NameError: name 'a' is not defined.但是,我在我的第一个函数中定义了 'a' 并在函数末尾返回它的值.

I am trying to write a program that checks Fermat's Last Theorem. The issue I am running into is that an error appears saying "NameError: name 'a' is not defined. However, I define 'a' in my first function and return its value at the end of the function.

我试图在第二个函数中使用第一个函数的输入值,以便用户可以定义参数.

I am trying to use the inputed values from the first function in the second function so the user can define the parameters.

我是否误解了如何利用回报"?非常感谢所有帮助,这将使我保持清醒.

Am I misunderstanding how to leverage "Return"? All help is greatly appreciate and will keep me sane.

def input_fermat():
    a=input('Enter the first variable \'a\': \n')
    b=input('Enter the second variable \'b\': \n')
    c=input('Enter the third variable \'c\': \n')
    n=input('Enter the exponential variable \'n\': \n')

    return a, b, c, n

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

    calc_1=a**n
    calc_2=b**n
    calc_3=c**n

    if n>2 and int(calc_1) + int(calc_2) == calc_3:
        print('Holy smokes, Fermat was wrong!')
    else:
        print('No that doesn\'t work.')

input_fermat()
check_fermat(a,b,c,n)

推荐答案

您没有存储函数 input_fermat 返回的值.试试:

You are not storing the values that the function input_fermat returns. Try:

a, b, c, n = input_fermat()

check_fermat(a,b,c,n)

这篇关于Python:返回可能不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆