Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing Required Positional Arguments

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!

like image 471
Madison Tetreault Avatar asked Mar 12 '26 18:03

Madison Tetreault


1 Answers

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)

like image 109
Taku Avatar answered Mar 15 '26 08:03

Taku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!