Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
Source Link
mayo
  • 501
  • 4
  • 6

Some PEP-8 details:

1.- According PEP-8 between each function you have to have two blank lines.

2.- Space between parameters (ie def check_fermat(a, b, c, n): instead of def check_fermat(a,b,c,n):. Source

3.- End of file it should be an new empty line.

4.- There is an extra space between print and ("Not a string, a number please.")

Related with the code;

5.- Instead of checking for 'y' or 'Y' you can apply lowercase to user input ( if str.lower(prompt) == "y":)

6.- a = int(a) is redundant, same for b, c and n.

7.- According your code n it should be higher than 2, is this correct?

8.- You are repeating the user input code, you should create a new function for that.

9.- Check fermat should just do that, so, program_restart should be handled in another place.

10.- That return on program_restart is unnecessary.

11.- If inside program_restart you have another call to user_input and inside this second user_input another call to program_restart and so on.. (You are falling into a recursion. Recursion per se is not bad, but in this case it is because you are trying to just repeat the program, no to solve some problema trough recursion), a better approach is control that with a loop.

12.- Add if __name__ == '__main__':

def program_restart():
 print("Would you like to try again? ")
 prompt = input("type 'Y' to continue or any other character to exit:\n")
 return str.lower(prompt) == "y"
def check_fermat(a, b, c, n):
 if a ** n + b ** n == c ** n:
 print("Holy Smokes, Fermat was wrong!")
 else:
 print("No, that does not work!")
 print("---------------------------")
def ask_number(text, bigger_than=0):
 number = bigger_than - 1
 while number <= bigger_than:
 try:
 number = int(input(text))
 except ValueError:
 print("Please write a valid number.")
 return number
def user_input():
 run_program = True
 while run_program:
 print("Let's see if Fermat was right. He claims that a^n+b^n=c^n cannot be True!")
 a = ask_number(text='Give a positive number bigger than 2 for "a":\n', bigger_than=2)
 b = ask_number(text='Give a positive number bigger than 2 for "b":\n', bigger_than=2)
 c = ask_number(text='Give a positive number bigger than 2 for "c":\n', bigger_than=2)
 n = ask_number(text='Give a positive number for the exponent "n":\n')
 check_fermat(a, b, c, n)
 run_program = program_restart()
if __name__ == '__main__':
 user_input()
lang-py

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