不知道如何使用创建的方法

我对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

我希望应用程序读取到处,如果用户键入“退出”将退出,即使它要求一个号码,以便我创建了2种方法来这样做,因为它不工作与循环和用户输入直接从主()处理

def Num1(): 

while True:

num1 = input("What is the first number? ")

if num1.isdigit():

break

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")

continue

def Num2():

while True:

num2 = input("What is the second number? ")

if num2.isdigit():

break

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")

continue

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

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

回答:

区分大小写,则定义Num1() & Num2()num1() & num2()

回答:

有很多你的代码错误。

  1. DRY不要重复自己。当他们做同样的事情时,不需要有两个功能。完整删除Num2。没用的。

  2. Num1没有返回值。我们希望函数向用户询问一个数字(它是这样做的),如果用户输入“exit”(也完成)并返回用户输入的数字(未完成),则退出。

  3. 函数名称以小写字母开头并且是描述性的。 Num1既不会,所以让我们将其更改为ask_number。

上述所有问题都可以通过用下面的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")

  • 你的主要方法,没有分配的函数的返回值到变量。通过
    num1 = ask_number() 

    num2 = ask_number()

    我们两次调用ask_number功能和指派其返回值NUM1和NUM2取代旧代码

    Num1() 

    num1 = int.num1()

    Num2()

    num2 = int.num2()

  • 修复它。

    回答:

    问题是Num1()和Num2()提出了问题,但没有对它们做任何事情。另外,定义的num1和num2是函数本地的变量,因此您无法通过main()函数访问它们。您应该添加“return numX”而不是break,并将num1()分配给num1,从而允许您将用户的输入捕捉到main()函数可访问的变量中。

    这是你的代码应该看起来像:

    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()(这两者都给我错误)等等。如果你想有返回到处理上的主要方法的价值,您需要做的return num1当您检查​​,并在你的主要方法设置firstNumberInput = Num1()

    希望这有助于!

    回答:

    这可能只是因为你已经发布了你的代码并有遗漏,但有几点需要解决。

    函数Num1和Num2不返回任何内容。虽然值num1是在函数内分配的,但它不能在该函数的范围之外访问。

    INT没有一个方法“NUM1”所以调用int.num1()应为int更换(NUM1())

    创建2个几乎相同的功能,为自己创造更多的工作,并使得代码很难改变,你可以创建一个更通用的功能。

    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())

    回答:

    我看到一些一般性的错误,在你的代码:

    1. 你调用函数NUM1()进入NUM1,但NUM1是当地 变量。所以你只能在Num1()函数中看到并使用它。与num2相同的 。所以,我建议使用Num1和Num2,但是要返回num1 和num2。
    2. 我推荐使用1个函数,不需要使用2.
    3. 看起来像用num1 = int.num1()你想把num1()转换成int。这是错误的。正确将会像num1 = int(num1)一样。 这会将num1从字符串转换为int并将其保存为num1。但 最好使用字符串和int的不同名称:num1 = int(num1_str)。而当你使用括号num1()时,这意味着你需要调用 num1函数来完成。但是num1不是一个函数,所以它不是可调用的 。
    4. 您使用将字符串转换为int两次。在编程中,当你看到两个代码,它们只是复制/粘贴时,最好为它制作一个 函数。这里我们有这个函数,所以只需在函数中插入 转换为int即可。
    5. 不要忘记缩进。这是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

    以上是 不知道如何使用创建的方法 的全部内容, 来源链接: utcz.com/qa/257484.html

    回到顶部