Could someone suggest ways to simplify this code, or any other tips? All help is much appreciated. Moreover, should I comment code above or below the line of code (I have to comment as this is a requirement)?
#This imports the time and random modules
import time, random
#This defines a function called comment
def comment():
#This makes the variable startage a global variable rather than a local variable
global startage
#This checks if choice is equal to 1 or equal to 2
if choice == 1 or choice == 2:
if startage < 25:
print("You're a young cat or dog")
elif startage > 25 and startage < 50:
print("You're a middle aged cat or dog")
elif startage > 50:
print("You're an old cat or dog")
elif choice == 3 or choice == 4:
if newage < 25:
print("You're a young cat or dog")
elif newage > 25 and startage < 50:
print("You're a middle aged cat or dog")
elif newage > 50:
print("You're an old cat or dog")
else:
pass
print("Welcome to the animal age calculator")
while True:
time.sleep(1)
choice = int(input("Which age would you like to convert?(Enter the corresponding number)\n1. Human ---> Dog\n2. Human ---> Cat\n3. Dog ---> Human\n4. Cat ---> Human\n5. Generate a random human age\n"))
time.sleep(1)
if choice >= 1 and choice <= 4:
startage = int(input("What age would you like to start with?\n"))
if choice == 1:
print("You have chosen Human ---> Dog and the Human age is", startage)
if startage < 3:
newage = startage*11
else:
newage = 22+(startage-2)*4
elif choice == 2:
print("You have chosen Human ---> Cat and the Human age is", startage)
if startage == 1:
newage = 15
else:
newage = 25+(startage-2)*4
elif choice == 3:
print("You have chosen Dog ---> Human and the Dog age is", startage)
if startage < 33:
newage = startage/11
else:
newage = (((startage-22)/4)+2)
elif choice == 4:
print("You have chosen Cat ---> Human and the Cat age is", startage)
if startage == 15:
newage = 1
else:
newage = (((startage-22)/4)+2)
elif choice == 5:
print("You have chosen to generate a random human age.")
time.sleep(1)
randomage = random.randint(1,122)
print(randomage)
time.sleep(1)
if choice >= 1 and choice <= 4:
print("Your new age is:", newage)
time.sleep(1)
comment()
restart = (input("Would you like to restart?(Yes or No)")).lower()
while True:
if restart == "yes":
pass
elif restart=="no":
break
return
the outcome of the function. \$\endgroup\$ – Akil Riaz May 4 '17 at 13:26