Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Don';我不知道如何使用创建的方法_Python - Fatal编程技术网

Python Don';我不知道如何使用创建的方法

Python Don';我不知道如何使用创建的方法,python,Python,我对Python和一般编程非常陌生。我正在跟随youtube上的一个例子,讲述如何制作一个简单的计算器,但我想添加我的想法和功能。或多或少,我想让它变得多才多艺 这是一个不适合我的部分。我不知道如何使用我创建的两种方法使其工作 def main(): while True: Num1() num1 = int.num1() Num2() num2 = int.num2() # Executing the specified calculations wit

我对Python和一般编程非常陌生。我正在跟随youtube上的一个例子,讲述如何制作一个简单的计算器,但我想添加我的想法和功能。或多或少,我想让它变得多才多艺

这是一个不适合我的部分。我不知道如何使用我创建的两种方法使其工作

def main():
while True:
    Num1()
    num1 = int.num1()
    Num2()
    num2 = int.num2()


  # Executing the specified calculations with the 'Operation' function (row 13)
    Operation(num1, num2)

     #  --> followed by another nested while asking if the user wants to make another calculation
我希望应用程序在任何地方都能读到,如果用户键入“exit”,它将退出,即使它需要一个数字,所以我创建了两个方法来这样做,因为它无法直接从main()处理循环和用户输入

问题是,我得到了这个错误,我得到了它,但我只是不知道如何去做

Traceback (most recent call last):
File "C:/Users/Albert/PycharmProjects/HelloWorld - First Py' 
Apps/SimpleCalculator.py", line 131, in <module>
main()
File "C:/Users/Albert/PycharmProjects/HelloWorld - First Py' 
Apps/SimpleCalculator.py", line 109, in main
Operation(num1, num2)
NameError: name 'num1' is not defined
回溯(最近一次呼叫最后一次):
文件“C:/Users/Albert/PycharmProjects/HelloWorld-First Py”
Apps/SimpleCalculator.py”,第131行,在
main()
文件“C:/Users/Albert/PycharmProjects/HelloWorld-First Py”
Apps/SimpleCalculator.py”,第109行,主视图
操作(num1,num2)
NameError:未定义名称“num1”

区分大小写,您定义的
Num1()&Num2()
不是
Num1()&Num2()
您的代码有很多错误

  • 不要重复你自己。当它们做相同的事情时,不需要有两个函数。全部删除Num2。没用

  • Num1没有返回值。我们希望函数要求用户输入一个数字(确实如此),如果用户输入“exit”(完成),则退出,并返回用户输入的数字(未完成)

  • 函数名以小写字母开头,具有描述性。Num1两者都没有,所以让我们把它改为询问_编号

  • 通过使用以下ask_number函数替换Num1和Num2,可以修复上述所有问题

    def ask_number():
        while True:
            num = input("What is the first number? ")
            if num.isdigit():
                return int(num) #This line fixes problem 2
            elif num == 'exit':
                print("Thank you using the SimpleCalculator. Bye!")
                time.sleep(3)  
                exit()  
            else:
                print("I don't understand. Please input a valid number")
    
    def getnum():
        while True:
            mynum = input("What is the first number? ")
            if mynum.isdigit():
                break
            elif mynum == 'exit':
                print("Thank you using the SimpleCalculator. Bye!")
                time.sleep(3)
                exit()
            else:
                print("I don't understand. Please input a valid number")
        return mynum
    
    def main():
        while True:
            num1 = int(getnum())
            num2 = int(getnum())
    
  • 主方法没有将函数的返回值赋给变量
  • 通过替换旧代码来修复它

    Num1()
    num1 = int.num1()
    Num2()
    num2 = int.num2()
    

    我们两次调用ask_number函数,并将其返回值赋给num1和num2。

    问题是num1()和num2()提出了问题,但没有对它们做任何处理。此外,所定义的num1和num2是函数的局部变量,因此无法从main()函数访问它们。您应该添加“return numX”而不是break,并将Num1()赋值给Num1,这样可以在main()函数可访问的变量中捕获用户的输入

    def ask_number():
        while True:
            num = input("What is the first number? ")
            if num.isdigit():
                return int(num) #This line fixes problem 2
            elif num == 'exit':
                print("Thank you using the SimpleCalculator. Bye!")
                time.sleep(3)  
                exit()  
            else:
                print("I don't understand. Please input a valid number")
    
    def getnum():
        while True:
            mynum = input("What is the first number? ")
            if mynum.isdigit():
                break
            elif mynum == 'exit':
                print("Thank you using the SimpleCalculator. Bye!")
                time.sleep(3)
                exit()
            else:
                print("I don't understand. Please input a valid number")
        return mynum
    
    def main():
        while True:
            num1 = int(getnum())
            num2 = int(getnum())
    
    您的代码应该是这样的:

    import time
    def Num1():
        while True:
            num1=input("What is the first number? ")
            if num1.isdigit():
                return int(num1)
            elif num1 == 'exit':
                print("Thank you using the SimpleCalculator. Bye!")
                time.sleep(3)  
                exit()  
            else:
                print("I don't understand. Please input a valid number")
    
    def Num2():
        while True:
            num2=input("What is the first number? ")
            if num2.isdigit():
                return int(num2)
            elif num2 == 'exit':
                print("Thank you using the SimpleCalculator. Bye!")
                time.sleep(3)  
                exit()  
            else:
                print("I don't understand. Please input a valid number")
    
    def Operation(num_1, num_2):
        # code for Operation function
        pass
    def main():
        while True:
            num1=Num1()
            num2=Num2()
            Operation(num1, num2)
    
    if __name__=="__main__": main()
    

    免责声明:我想要求一些澄清,但我还没有SO特权

    运行程序时,能否确保缩进正确?例如,while循环不应与函数定义处于同一缩进级别

    另外,您的函数都在这个文件
    SimpleCalculator.py
    中吗?如果是这样的话,您将需要有一个额外的行来调用您的方法,因为现在它们只被声明

    if __name__ == '__main__':
        # And the following line will call your main function which you defined previously        
        main() 
    
    更好的是,只需删除
    main()
    函数,并用更具python风格的语法替换它:

    if __name__ == '__main__':
        while True:
            Num1()
            Num2()
    
    另外,由于您正在调用
    Num1()
    Num2()
    并且它们正在处理输入,因此您不需要设置
    Num1=int.Num1()
    num=int.Num2()
    (这两个都会给我错误)或类似的设置。如果希望将值返回到main方法进行处理,则需要在选中
    If num1.isdigit():
    时执行
    return num1
    ,并在main方法集中执行
    firstNumberInput=num1()


    希望这有帮助

    可能只是您发布了代码,但有几点需要解决

    函数Num1和Num2不返回任何内容。尽管num1值是在函数内分配的,但在该函数的作用域之外是不可访问的

    int没有方法“num1”,因此调用int.num1()应替换为int(num1())

    创建两个几乎相同的函数会为您自己创建更多的工作,并且代码很难更改,您可以创建一个更通用的函数

    def ask_number():
        while True:
            num = input("What is the first number? ")
            if num.isdigit():
                return int(num) #This line fixes problem 2
            elif num == 'exit':
                print("Thank you using the SimpleCalculator. Bye!")
                time.sleep(3)  
                exit()  
            else:
                print("I don't understand. Please input a valid number")
    
    def getnum():
        while True:
            mynum = input("What is the first number? ")
            if mynum.isdigit():
                break
            elif mynum == 'exit':
                print("Thank you using the SimpleCalculator. Bye!")
                time.sleep(3)
                exit()
            else:
                print("I don't understand. Please input a valid number")
        return mynum
    
    def main():
        while True:
            num1 = int(getnum())
            num2 = int(getnum())
    

    我在您的代码中看到一些常见错误:

  • 您可以调用函数Num1()来输入Num1,但Num1是本地值 变量因此,您只能在Num1()函数中看到并使用它。相同的 使用num2。因此,我建议使用Num1和Num2,但返回Num1 和num2
  • 我建议使用1功能,没有必要使用2
  • 看起来,对于num1=int.num1(),您希望将num1()转换为int。这是错误的。正确值将类似于num1=int(num1)。 这将num1从字符串转换为int,并将其保存到num1。但是 最好对字符串和int使用不同的名称:num1= int(num1_str)。当您使用括号num1()时,这意味着您调用 要完成的num1函数。但是num1不是一个函数,所以它不是 可呼叫
  • 可以使用两次将字符串转换为int。在编程中,当您看到两个代码(只是复制/粘贴)时,最好创建一个 它的功能。这里有函数,只需插入 在函数中转换为int
  • 不要忘记缩进。这是Python中最重要的事情。在其他语言中,可以使用类似{}的内容来定义 某个逻辑块的开始和结束。但在Python中,它 一切都取决于缩进
  • 因此,使用这个,它可以如下所示:

    def main():
        while True:
            # just call NumEntering - one general function for entering numbers,
            # and give to it attribute - message for user.
            num1 = int(NumEntering("What is the first number? "))
            num2 = int(NumEntering("What is the second number? "))
    
            # Executing the specified calculations with the 'Operation' function (row 13)
            Operation(num1, num2)
    
    以及功能:

    def NumEntering(message):  # message - this what function get from outer function.
        while True:
            num_str = input(message)  # show given message
            if num_str.isdigit():
                return int(num_str)  # convert to int and return to outer function
            elif num_str == 'exit':
                print("Thank you using the SimpleCalculator. Bye!")
                time.sleep(3)  
                exit()  
            else:
                print("I don't understand. Please input a valid number")
                continue
    

    嗨,谢谢你的意见。我听从了你的建议。工作起来很有魅力。就像我想要的一样。干杯嗨,非常感谢。谢谢,所有函数都在同一个文件中。然而,我现在正在从另一个程序中使用它,是的,我包括了从外部调用方法的行,谢谢!