Python script for Gregorian Epact

Viewed 32

I want to write a script that will return the Gregorian epact. The program asks the user to provids a year. It checks if the input is correct (i.e. number). It determines the number of digits of the given number (4 here). If the user fails five to provides a correct input the scripts terminates. Here is my script. I think it is rather complicated. How could I simplify it?

# Program for the calculation of the Gregorian epact
import math as m  # Python's basic mathematical library

print("This program calculates the Gregorian epact value of a given year.")
print("See https://fr.wikipedia.org/wiki/%C3%89pacte for further details.")

is_year_correct = False
n = 0
while (not is_year_correct) and n<=4:  
  year = input("Enter the year (e.g. 2020): ")  # User provides a year
  try:
    year = int(year)
    digits = int(m.log10(year))+1  # number of digits in year
  except ValueError:
    print("Please, try again by entering a year!")
  if digits == 4:
    is_year_correct = True
  else:
    print("Please enter a four digit number.")
  n = n+1

if n<=4:
    c = year // 100
    epact = (8+(c//4) - c + ((8*c + 13)//25) + 11 * (year % 19)) % 30
    print("The epact value is", epact, "days.")
else:
  print('No more attempts!')
1 Answers

Wow learning about epact led me on a rabbit hole I have minimized the code a bit however I lost some functionality... I have a way to make sure the functionality is ensured and I will share the piece of code in a sec:

Code with comments:

# Program for the calculation of the Gregorian epact
print("This program calculates the Gregorian epact value of a given year.")
print("See https://fr.wikipedia.org/wiki/%C3%89pacte for further details.")

# Infinite loop
while True:  
 # Tries to convert the user's input into a number
try:
 year = int(input("Enter the year (e.g. 2020): "))  # User provides a year
 # All code under this will NOT be executed if user doesn't enter a number
 # as the code above will raise an exception causing us to go to the 
 # exception area

# If year is less than 1000 we warn else we leave infinite loop
if year < 1000: print("Please enter a four digit number.")
else: break
# If user enters a letter
except ValueError:
 print("Please, try again by entering a year!")
# Epact calculations
c = year // 100
epact = (8+(c//4) - c + ((8*c + 13)//25) + 11 * (year % 19)) % 30
print(f"The epact value is {epact} days.")

Code without Comments:

# Program for the calculation of the Gregorian epact
print("This program calculates the Gregorian epact value of a given year.")
print("See https://fr.wikipedia.org/wiki/%C3%89pacte for further details.")
while True:  
 try:
  year = int(input("Enter the year (e.g. 2020): "))  # User provides a year
  if year < 1000: print("Please enter a four digit number.")
  else: break
 except ValueError:
  print("Please, try again by entering a year!")
c = year // 100
epact = (8+(c//4) - c + ((8*c + 13)//25) + 11 * (year % 19)) % 30
print(f"The epact value is {epact} days.")

All functionality

# Program for the calculation of the Gregorian epact
print("This program calculates the Gregorian epact value of a given year.")
print("See https://fr.wikipedia.org/wiki/%C3%89pacte for further details.")

for _ in range(5):
 try:
  year = int(input("Enter the year (e.g. 2020): "))  # User provides a year
  if year < 1000: print("Please enter a four digit number.")
  else: break
 except ValueError:
  print("Please, try again by entering a year!")

try:
 c = year // 100
 epact = (8+(c//4) - c + ((8*c + 13)//25) + 11 * (year % 19)) % 30
 print(f"The epact value is {epact} days.")
except: print("No more tries!")
Related