Reputation: 1
Creating a function which is based on fermat's last theorem, how to take assign user input to other function and then evaluate it?
def take_input(a,b,c,n):#take input from user
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
n = int(input("Enter n: "))
c = c**n
return n
def check_fermat(n):#evaluate the input and print results
if n > 2:
if c == a**n + b**n:
print("Holy smokes, Fermat was wrong!")
else:
print("No, that doesn’t work.")
else:
print(n, "enter greater than this.")
check_fermat(take_input)
Upvotes: 0
Views: 799
Reputation: 891
For greater flexibility I would change the interfaces of the functions in the following way. The input function should return all input values and do not do any calculation. The check function should take all relevant values as parameters.
def take_input():#take input from user
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
n = int(input("Enter n: "))
return (a, b, c, n)
def check_fermat(a, b, c, n):#evaluate the input and print results
if n > 2:
if c**n == a**n + b**n:
print("Holy smokes, Fermat was wrong!")
else:
print("No, that doesn’t work.")
else:
print("Invalid value for n:", n)
(a, b, c, n) = take_input()
check_fermat(a, b, c, n)
Another possibility would be to move the check for illegal values of n
to the input function so it only returns valid inputs.
Upvotes: 1
Reputation: 7361
There are several ways to combine the two functions, the correct way depends on what you want. @Hari_Sheldon's answer uses nested function. In this case check_fermat()
can be used only in the body of take_input()
function.
Maybe you want two independent functions. Imagine a situation where a function collects the input, and this input can be used by different functions to perform different tasks, calculations, or whatever. Being the input the same, make sense to use a separate function so that you do not have to repeat yourself. In your example (checking Fermat's Last Theorem) you could write:
def take_input(): #take input from user
i_a = int(input("Enter a: "))
i_b = int(input("Enter b: "))
i_c = int(input("Enter c: "))
i_n = int(input("Enter n: "))
return i_a, i_b, i_c, i_n
def check_fermat(): #evaluate the input and print results
a, b, c, n = take_input()
if n > 2:
if c**n == a**n + b**n:
print("Holy smokes, Fermat was wrong!")
else:
print("No, that doesn’t work.")
else:
print(n, "enter greater than this.")
check_fermat()
You do not need to pass as argument the variables which you want from the user (the ones obtained with input()
), they will be overwritten in any case. But the function which reads them, must return them otherwise they will be lost.
Upvotes: 2
Reputation: 969
def take_input(a,b,c,n):#take input from user
a = int(input("Enter a: ")) ##Indentation should be strictly followed in python
b = int(input("Enter b: "))
c = int(input("Enter c: "))
n = int(input("Enter n: "))
c = c**n
def check_fermat():#evaluate the input and print results
if n > 2:
if c == a**n + b**n:
print("Holy smokes, Fermat was wrong!")
else:
print("No, that doesn’t work.")
else:
print(n, "enter greater than this.")
check_fermat()
return n ##return should be last otherwise check_fermat cannot be called
Upvotes: 0