Passing input between two functions
Question:
Need to write a code that satisfies this requirement: "Your program will contain two functions. The first should be a function that asks the user for two numbers and passes those numbers to main() which passes them to a second function. The second function should multiply those numbers together. Make sure to include a caller after and a docstring in all of your functions."
Here is what I have so far, getting an error saying my x-value is not defined.
def main():
def multiplicationfunc(x,y):
'''this function multiplies the user input'''
z = x * y
return z
def user_input():
'''recieve two integers that will be used to multiply'''
x = eval(input("enter the first integer:"))
y = eval(input("enter the second integer:"))
return x,y
multiplicationfunc(x,y)
user_input()
main()
How do I fix it within the question?
Answers:
If I understand correctly, here is a fix to your issue:
def multiplicationfunc(x,y):
'''this function multiplies the user input'''
return x * y
def main(x, y):
return multiplicationfunc(x, y)
def user_input():
'''recieve two integers that will be used to multiply'''
x = int(input("enter the first integer:"))
y = int(input("enter the second integer:"))
return main(x, y)
print(user_input())
What I am doing here is first defining the multiplicationfunc()
function, that takes two parameters, x
and y
, and then returning the product of them. Then I am defining the main function which also takes two parameters and then calls the multiplicationfunc()
with the two parameters. Then I make the user_input()
function that with take two integers and then return the result from the main()
function. Then I just print what user_input()
returns.
Need to write a code that satisfies this requirement: "Your program will contain two functions. The first should be a function that asks the user for two numbers and passes those numbers to main() which passes them to a second function. The second function should multiply those numbers together. Make sure to include a caller after and a docstring in all of your functions."
Here is what I have so far, getting an error saying my x-value is not defined.
def main():
def multiplicationfunc(x,y):
'''this function multiplies the user input'''
z = x * y
return z
def user_input():
'''recieve two integers that will be used to multiply'''
x = eval(input("enter the first integer:"))
y = eval(input("enter the second integer:"))
return x,y
multiplicationfunc(x,y)
user_input()
main()
How do I fix it within the question?
If I understand correctly, here is a fix to your issue:
def multiplicationfunc(x,y):
'''this function multiplies the user input'''
return x * y
def main(x, y):
return multiplicationfunc(x, y)
def user_input():
'''recieve two integers that will be used to multiply'''
x = int(input("enter the first integer:"))
y = int(input("enter the second integer:"))
return main(x, y)
print(user_input())
What I am doing here is first defining the multiplicationfunc()
function, that takes two parameters, x
and y
, and then returning the product of them. Then I am defining the main function which also takes two parameters and then calls the multiplicationfunc()
with the two parameters. Then I make the user_input()
function that with take two integers and then return the result from the main()
function. Then I just print what user_input()
returns.