Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Ask
- #Declare Variables
- oldestFriend = 0
- middleFriend = 0
- youngestFriend = 0
- #Function to sort ages (in ascending order)
- def sortFriends(Friend):
- #Reference variables outside function
- global youngestFriend
- global middleFriend
- global oldestFriend
- #if new friend is older than middle/inital friend
- if (Friend > middleFriend) and (oldestFriend == 0):
- oldestFriend = Friend
- #if new friend is the same age or younger than middle/inital friend
- elif (Friend <= middleFriend) and (youngestFriend == 0):
- youngestFriend = Friend
- #if new friend is even older than the two previously entered friends
- elif (Friend > oldestFriend) and (oldestFriend != 0):
- youngestFriend = middleFriend
- middleFriend = oldestFriend
- oldestFriend = Friend
- #if new friend is even younger than the two previously entered friends
- elif (Friend <= youngestFriend) and (youngestFriend != 0):
- oldestFriend = middleFriend
- middleFriend = youngestFriend
- youngestFriend = Friend
- #if new friend is in between the two previously entered friends and older than middle
- elif (Friend > middleFriend) and (Friend < oldestFriend) and (oldestFriend !=0):
- youngestFriend = middleFriend
- middleFriend = Friend
- #if new friend is in between the two previously entered friends and younger than middle
- elif (Friend < middleFriend) and (Friend > youngestFriend) and (youngestFriend !=0):
- oldestFriend = middleFriend
- middleFriend = Friend
- #Start with first friend in the middle
- middleFriend = Ask.forInt("What's the age of one of your friends?")
- #and compare following friends with inital friend
- sortFriends(Ask.forInt("What's the age of another one of your friends?"))
- sortFriends(Ask.forInt("What's the age of a third friend of yours?"))
- equalAgeGap = False
- if (middleFriend - youngestFriend) == (oldestFriend - middleFriend):
- equalAgeGap = True
- if equalAgeGap:
- print("The ages of your friends are {0} years apart.".format(middleFriend - youngestFriend))
- else:
- print("Your friends ages are not the same.")
Add Comment
Please, Sign In to add comment