简体   繁体   English

我的“非常简单”的Python计算器出现问题

[英]Issue with my 'very simple' Python Calculator

I have written another simple program that I call my basic 'Python Calculator.' 我编写了另一个简单的程序,称之为基本的“ Python计算器”。 So far, I have found a problem with some of the code that I have written in Python 3.3.2. 到目前为止,我发现用Python 3.3.2编写的某些代码存在问题。 Here is the code below so you can read it before I state my problem... 这是下面的代码,因此您可以在我陈述问题之前阅读它。

def startup_screen():
    user_input_1 = int(input("Please enter your first number.\n"))
    user_input_2 = int(input("Now please enter your second number.\n"))
    command_list()

def command_list():
    user_command = input("Now what would you like to do with these two numbers?\n").lower()

    if user_command == "add":
        calc_add()
    elif user_command == "subtract":
        calc_subtract()
    elif user_command == "multiply":
        calc_multiply()
    elif user_command == "divide":
        calc_divide
    else:
        print("Please try again.")
        user_command

def calc_add():
    add_result = (user_input_1+user_input_2)
    print(add_result)
    command_list()

def calc_subtract():
    subtract_result = (user_input_1-user_input_2)
    print(subtract_result)
    command_list()

def calc_multiply():
    multiply_result = (user_input_1*user_input_2)
    print(multiply_result)
    command_list()

def calc_divide():
    divide_result = (user_input_1/user_input_2)
    print(divide_result)
    command_list()

startup_screen()

Here, you can see that I have defined the startup_screen , which collects user input as an integer and then stores it respectively in user_1 and user_2 . 在这里,您可以看到我已经定义了startup_screen ,它将用户输入收集为一个整数,然后分别将其存储在user_1user_2 Then, the user is taken to the command home screen where they can choose to either add, subtract, multiply or divide both numbers. 然后,用户被带到命令主屏幕,他们可以选择对两个数字进行加,减,乘或除。 The command is then determined from the user input, so if the user wants to add two numbers, they type in 'add' and they are taken to the add function - as defined above. 然后从用户输入中确定命令,因此,如果用户要添加两个数字,则键入“ add”,然后将它们带到add函数-如上所定义。

However , in the add function, the user_1 and user_2 inputs are not recognized, as well as in the other programs... so it causes an error in the in the program. 但是 ,在添加功能中,在其他程序中以及其他程序中都无法识别user_1和user_2输入...因此会在程序中导致错误。 I have no idea what to do. 我不知道该怎么做。 Could you please help me? 请你帮助我好吗?

Thank you very much... 非常感谢你...

Give the inputs as arguments to the functions that follow, the values are not global variables (not immediately accessible by any function) if they are defined within a function. 将输入作为后续函数的参数,如果它们在函数中定义,则这些值不是全局变量(任何函数都不能立即访问)。

def loadscreen():
    ...
    command_home(user_input_1,user_input_2)

def command_home(user_input_1,user_input_2):
    ...
    calc_add(user_input_1,user_input_2)

def calc_add(user_input_1,user_input_2)
    add_result = (user_input_1 + user_input_2)
    print(add_result)

Lets look at you program from the point of view of the def calc_add(): function. 让我们从def calc_add():函数的角度来看您的程序。

In the first line, 在第一行中

add_result = (user_input_1 + user_input_2)

you say, take the value of user_input_1 add it to user_input_2 and make that into the variable add_result . 您说,将user_input_1的值添加到user_input_2并将其添加到变量add_result Okay that makes sense. 好吧,那是有道理的。

Now lets get the first value user_input_1 ... lets see, first place to check is the 'local scope'. 现在让我们获取第一个值user_input_1 ...让我们看一下,首先要检查的是“本地范围”。 That is the function i am currently in. 那就是我目前正在使用的功能。

def calc_add():
    add_result = (user_input_1 + user_input_2)
    print(add_result)

Okay nope, nothing there... okay maybe it was one of the input parameters ? 好吧,不,那里什么都没有...好吧,也许这是输入参数之一?

def calc_add():

Nope no input parameters were passed to this function. 不,没有输入参数传递给该函数。 Okay it must be global ... but wait crap... no global parameters are ever decalred! 好的,它必须是全局的……但是请放心……没有全局参数被贴花! especially not user_input_1 . 尤其是user_input_1 Well dang it, im supposed to add two parameters but one of the parameters doesnt exist! 好吧,我应该添加两个参数,但是其中一个参数不存在! Thats not possible I better throw an error! 那不可能,我最好抛出一个错误!

Maybe if the programmer had passed me the information i needed it would work better, Maybe something like this : 也许如果程序员将我需要的信息传递给我,它会更好地工作,也许是这样的:

def load_screen():
    user_1 = int(input("Please enter your first number.\n"))
    user_2 = int(input("Now please enter your second number.\n"))
    command_home( user_1, user_2) # Okay command_home I took 2 inputs! 
                                  # Here they are work your magic!



def command_home(in1, in2): # Hey I need 2 varaibles to work, Ill call them in1 and in2.
                            # so based on the way command_home( user_1, user_2) is called
                            # in1 = user_1, in2 = user_2 
    command = input("Now what would you like to do with these two numbers?\n").lower()
    if command == "add":
        calc_add(in1, in2) #Okay, now i need to add my two inputs,
                           # but calc_add is another function it wont be able to see MY
                           # in1, in2 ...  i better pass them over as parameters.
    elif command == "subtract":
        calc_subtract()
    elif command == "multiply":
        calc_multiply()
    elif command == "divide":
        calc_divide()



def calc_add(user_input_1,user_input_2 ): # Okay now i know where to look for user_input_1 
                                          # and user_input_2, they will be passed to me
                                          # when someone calls me! Now i can find my varables
                                          # and i wont throw an error!
    add_result = (user_input_1 + user_input_2)
    print(add_result)

You are not passing the user's input into the later functions which rely on it, such as calc_add(). 您不会将用户的输入传递给依赖它的更高版本的函数,例如calc_add()。

The way to fix this problems would be to modify your code like so: 解决此问题的方法是像这样修改您的代码:

def startup_screen():
    user_1 = int(input("Please enter your first number.\n"))
    user_2 = int(input("Now please enter your second number.\n"))
    command_home(user_1,user_2)

def command_home(u1,u2):
    command = input("Now what would you like to do with these two numbers?\n").lower()
    if command == "add":
        calc_add(u1,u2)
    elif command == "subtract":
        calc_subtract(u1,u2)
    elif command == "multiply":
        calc_multiply(u1,u2)
    elif command == "divide":
        calc_divide(u1,u2)

def calc_add(user_input_1,user_input_2):
    add_result = (user_input_1 + user_input_2)
    print(add_result)

"""the rest of the functions go here, must be defined to take the inputs passed"""

startup_screen()

We have redefined command_home to take two objects as inputs (which should be the user_1 and user_2 from load_screen ). 我们已经重新定义了command_home以将两个对象用作输入(应该是load_screenuser_1user_2 )。 You could call them anything but I called them u1 and u2 so I wouldn't have to type so much. 您可以称呼它们为任何东西,但我称呼它们为u1u2因此不必键入太多内容。 Now, command_home has these values and can, in turn, pass them into the functions which it calls that need them. 现在, command_home具有这些值,然后可以将它们传递到它需要的函数中。 We have also changed all of the function calls inside the if / elif block so that u1 and u2 are passed into those functions. 我们还更改了if / elif块内的所有函数调用,以便将u1u2传递到这些函数中。

Then, we have redifined calc_add to take two inputs, and I called them the same as you did already inside the function, so I would have one less line to change - user_input_1 and user_input_2 . 然后,我们重新定义了calc_add以接受两个输入,并且我对它们的调用与您在函数内部所做的相同,因此我需要更改的行少了一行user_input_1user_input_2

Now when you ask for them to be added together into add_result , they exist and have the expected values, so everything now works. 现在,当您要求将它们一起添加到add_result ,它们就已经存在并具有期望值,因此一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM