Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/1501211140382244867
Became Hot Network Question
Formatting fixes
Source Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325

Checking Function that checks Fermat’s Theorem using functions and conditionals if, elif, elsefor a set of values

This is a challenge from Think Python 2nd EditionThink Python 2nd Edition:

Fermat’s Last Theorem says that there are no positive integers a, b, and c such that a^n + b^n = c^n. for any values of n greater than 2.

  1. Write a function named check_fermat that takes four parameters; a, b, c and n. And that checks to see if Fermat’s theorem holds. If n is greater than 2 and it turns out to be true that a^n + b^n = c^n the program should print, "Holy smokes, Fermat was wrong!" Otherwise the program should print, "No, that doesn’t work."

  2. Write a function that prompts the user to input values for a, b, c and n, converts them to integers, and uses check_fermat to check whether they violate Fermat’s theorem.

Fermat’s Last Theorem says that there are no positive integers a, b, and c such that an + bn = cn. for any values of n greater than 2.

  1. Write a function named check_fermat that takes four parameters; a, b, c and n. And that checks to see if Fermat’s theorem holds. If n is greater than 2 and it turns out to be true that an + bn = cn the program should print, "Holy smokes, Fermat was wrong!" Otherwise the program should print, "No, that doesn’t work."

  2. Write a function that prompts the user to input values for a, b, c and n, converts them to integers, and uses check_fermat to check whether they violate Fermat’s theorem.

That's what I've done so far. Are there any improvements I should make? And are there any 'best practices' among programmers that I'm missing? I'm just starting out and I'd like to avoid bad habits.

def check_fermat(a, b, c, n):
 if n > 2 and a**n + b**n == c**n:
 print("Holy smokes! Fermat was wrong!")
 elif n <= 2:
 print("The exponent should be grater than '2'")
 else:
 print("No, that doesn't work.")
def check_numbers():
 a = int(input("Choose a number for 'a': "))
 b = int(input("Choose a number for 'b': "))
 c = int(input("Choose a number for 'c': "))
 n = int(input("Choose a number for 'n' that's greater than '2': "))
 check_fermat(a, b, c, n)
check_numbers()

Checking Fermat’s Theorem using functions and conditionals if, elif, else

This is a challenge from Think Python 2nd Edition:

Fermat’s Last Theorem says that there are no positive integers a, b, and c such that a^n + b^n = c^n. for any values of n greater than 2.

  1. Write a function named check_fermat that takes four parameters; a, b, c and n. And that checks to see if Fermat’s theorem holds. If n is greater than 2 and it turns out to be true that a^n + b^n = c^n the program should print, "Holy smokes, Fermat was wrong!" Otherwise the program should print, "No, that doesn’t work."

  2. Write a function that prompts the user to input values for a, b, c and n, converts them to integers, and uses check_fermat to check whether they violate Fermat’s theorem.

That's what I've done so far. Are there any improvements I should make? And are there any 'best practices' among programmers that I'm missing? I'm just starting out and I'd like to avoid bad habits.

def check_fermat(a, b, c, n):
 if n > 2 and a**n + b**n == c**n:
 print("Holy smokes! Fermat was wrong!")
 elif n <= 2:
 print("The exponent should be grater than '2'")
 else:
 print("No, that doesn't work.")
def check_numbers():
 a = int(input("Choose a number for 'a': "))
 b = int(input("Choose a number for 'b': "))
 c = int(input("Choose a number for 'c': "))
 n = int(input("Choose a number for 'n' that's greater than '2': "))
 check_fermat(a, b, c, n)
check_numbers()

Function that checks Fermat’s Theorem for a set of values

This is a challenge from Think Python 2nd Edition:

Fermat’s Last Theorem says that there are no positive integers a, b, and c such that an + bn = cn. for any values of n greater than 2.

  1. Write a function named check_fermat that takes four parameters; a, b, c and n. And that checks to see if Fermat’s theorem holds. If n is greater than 2 and it turns out to be true that an + bn = cn the program should print, "Holy smokes, Fermat was wrong!" Otherwise the program should print, "No, that doesn’t work."

  2. Write a function that prompts the user to input values for a, b, c and n, converts them to integers, and uses check_fermat to check whether they violate Fermat’s theorem.

That's what I've done so far. Are there any improvements I should make? And are there any 'best practices' among programmers that I'm missing? I'm just starting out and I'd like to avoid bad habits.

def check_fermat(a, b, c, n):
 if n > 2 and a**n + b**n == c**n:
 print("Holy smokes! Fermat was wrong!")
 elif n <= 2:
 print("The exponent should be grater than '2'")
 else:
 print("No, that doesn't work.")
def check_numbers():
 a = int(input("Choose a number for 'a': "))
 b = int(input("Choose a number for 'b': "))
 c = int(input("Choose a number for 'c': "))
 n = int(input("Choose a number for 'n' that's greater than '2': "))
 check_fermat(a, b, c, n)
check_numbers()
Source Link

Checking Fermat’s Theorem using functions and conditionals if, elif, else

This is a challenge from Think Python 2nd Edition:

Fermat’s Last Theorem says that there are no positive integers a, b, and c such that a^n + b^n = c^n. for any values of n greater than 2.

  1. Write a function named check_fermat that takes four parameters; a, b, c and n. And that checks to see if Fermat’s theorem holds. If n is greater than 2 and it turns out to be true that a^n + b^n = c^n the program should print, "Holy smokes, Fermat was wrong!" Otherwise the program should print, "No, that doesn’t work."

  2. Write a function that prompts the user to input values for a, b, c and n, converts them to integers, and uses check_fermat to check whether they violate Fermat’s theorem.

That's what I've done so far. Are there any improvements I should make? And are there any 'best practices' among programmers that I'm missing? I'm just starting out and I'd like to avoid bad habits.

def check_fermat(a, b, c, n):
 if n > 2 and a**n + b**n == c**n:
 print("Holy smokes! Fermat was wrong!")
 elif n <= 2:
 print("The exponent should be grater than '2'")
 else:
 print("No, that doesn't work.")
def check_numbers():
 a = int(input("Choose a number for 'a': "))
 b = int(input("Choose a number for 'b': "))
 c = int(input("Choose a number for 'c': "))
 n = int(input("Choose a number for 'n' that's greater than '2': "))
 check_fermat(a, b, c, n)
check_numbers()
lang-py

AltStyle によって変換されたページ (->オリジナル) /