简体   繁体   中英

Missing Required Positional Arguments in Python

below is my an example of what i am trying to do in my code...

def func():
x = int (input ('enter x: '))
return x

def func2():
y = int (input( 'enter y: '))
return y

def func3(x,y):
print(randomint(x,y))

def main():
func()
func2()
func3()

main()

What i am wondering is, why cant i use the x and y variables that i have defined via input and returned at the end of my functions? When this program tries to run it says the functions are missing required arguments. Silly i know, i am new to python.

furthermore, how can i use variable in one function i am creating, that were defined within another separate function? thanks!

You stated that you know how to indent so I'm not going to discuss that, the problem at hand is that you will need to catch the return value from func and func2 after they are caught.

You can do so like this:

def func():
    x = int (input ('enter x: '))
    return x

def func2():
    y = int (input( 'enter y: '))
    return y

def func3(x,y): # there's two positional value so you will need to pass two values to it when calling
    print(randomint(x,y))

def main():
    x = func() # catch the x
    y = func2() # catch the y
    func3(x,y) # pass along x and y which acts as the two positional values
    # if you're lazy, you can directly do:
    # func3(func(), func2()) which passes the return values directly to func3

main()

Another method is to use the global statement, but that isn't the best way for your case.

Just a hint: if you are using the random module, the random integer is called by: random.randint(x,y)

Your variables only live within the functions, there is no way for func3 to get x and y, but you have defined x and y as parameters. So far you're just not passing them in. The following should do.

def func():
    x = int (input ('enter x: '))
    return x

def func2():
    y = int (input( 'enter y: '))
    return y

def func3(x,y):
    print(randomint(x,y))

def main():
    x_val = func()
    y_val = func2()
    func3(x_val, y_val)

main()

Or just like this, if you don't want to use variables.

Just remember, same name doesn't mean it's the same variable. The scope can be different (method, function, elsewhere), and the name makes the variable unique ("the same") withhin the same scope . That is similar across all higher programming languages, but also, scopes can intersect, and in different ways. So that reuse example above, might, for example work in JavaScript.

This is probably closest to what you attempted to achieve:

def inX():
    return int (input ('enter x: '))

def inY():
    return int (input( 'enter y: '))

def PrintRand(x,y): 
    print(randomint(x,y))

def main():
    PrintRand(InX(),InY()) # is probably closest to what you attempted to do.

main()

note that those slight renames do not have an effect other than understanding the code, but good names of methods telling what they actually do, are very important. You read the code many more times. You write it once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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