在函数内部定义变量

时间:2020-08-03 02:52:32

标签: python-3.x

我是python的新手,我昨天才开始学习。实际上,这是我的第一个类似于勾股定理的python项目。运行代码时,出现NameError: name 'variabel' is not defined错误。这是我的以下代码:

# Functions
def pythagoras_hypotenuse(variabel, variabel2) :
    answer = (variabel**2 + variabel2**2)**0.5
    return print("The length of the side is " + str(answer))

def pythagoras_sides(variabel, variabel2) :
    variabel = int(input("Enter the first side = "))
    variabel2 = int(input("Enter the second side = "))
    if variabel > variabel2 :
        return print("The length of the side is" + (variabel**2 - variabel2**2)**0.5)
    else :
        return print("The length of the side is" + (variabel2**2 - variabel**2)**0.5)

# A prompt to decide whether it's Hypotenuse or Sides
hypotenuse = input("Are you looking for hypotenuse? \nPlease respond with yes or no : ")
if hypotenuse == 'yes':
    pythagoras_hypotenuse(variabel, variabel2)
elif hypotenuse == 'no' :
    pythagoras_sides(variabel, variabel2)
else :
    print("Please respond with yes or no")

# Variables

variabel = int(input("Enter the first side = "))
variabel2 = int(input("Enter the second side = "))

有没有一种方法来定义变量,而不是首先询问它?我希望代码以斜边或边提示开始,然后输入边/斜边整数。我的代码很乱,进一步的解释和建议绝对会让我高兴。非常感谢! 注意:英语不是我的母语,对不起,英语不好!

3 个答案:

答案 0 :(得分:1)

是的。当您要求斜边时,将变量定义放在上面,并带有一些虚拟值:


...other code...

variabel = 3
variabel2 = 4

# A prompt to decide whether it's Hypotenuse or Sides
hypotenuse = input("Are you looking for hypotenuse? \nPlease respond with yes or no : ")
if hypotenuse == 'yes':
    pythagoras_hypotenuse(variabel, variabel2)
elif hypotenuse == 'no' :
    pythagoras_sides(variabel, variabel2)
else :
    print("Please respond with yes or no")

# Variables

variabel = int(input("Enter the first side = "))
variabel2 = int(input("Enter the second side = "))

答案 1 :(得分:0)

因此,您要完成的工作是-声明一个未初始化的变量,对于Python,由于其语法原因,这是不可能的。

var variabel1;  // value defaults to 'undefined'

与此等效的Pythonic是:

variabel1 = None

答案 2 :(得分:0)

您需要更多地研究python,因为您的代码过于混乱并且逻辑不清楚。而且,我想您尝试使用的单词是variable而不是variabel。您面临的问题仅仅是由于执行流程和范围的不正确使用。但是,一种更好的清洁方法如下

def get_side_inputs():
    return int(input("\nEnter the first side: ")), int(input("\nEnter the second side: "))

def get_user_choice():
    return input("\nAre you looking for hypotenuse? \nPlease respond with yes or no : ")

def pythagoras_hypotenuse(variable, variable2):
    answer = (variabel**2 + variabel2**2)**0.5
    print("\nThe length of the side is " + str(answer))

def pythagoras_sides(variable, variable2):
    if variable > variable2:
        print("\nThe length of the side is", (variable**2 - variable2**2)**0.5)
    else :
        print("The length of the side is", (variable2**2 - variable**2)**0.5)

def main():

    first_side, second_side = get_side_inputs()

    choice = get_user_choice()

    if choice == "yes":
        pythagoras_hypotenuse(first_side, second_side)
    
    elif choice == "no":
        pythagoras_sides(first_side, second_side)
    
    else: 
        print("Please respond with yes or no!!")

main()

一些提示:

  • 尝试使用main方法作为代码指南。任何人只要看一下主要方法就应该了解您的代码流程
  • 正确命名变量
  • 正确使用空格和空白行可以减少代码的混乱程度