In: Computer Science
Problem #2: Going to the Fair Assignment Goals: understanding
problem requirements, logical thinking, if-elif-else, and logical
operators.
Suppose the Great Frederick Fair wants to update its ticketing
software. They need you to write a program to handle the price
calculations, using the rules*:
• The basic price of a ticket is $40. • Senior citizens (age >=
65) get a 50% discount. • Children under 6 are free. • For
residents of Frederick County, the basic price is $35; the same
discounts still apply.
So the possible ticket prices are $0, $17.50, $20, $35, and $40.
Your program should request age and county name from the user. The
age will be entered as an integer and the county name as a
string.
Before calculating the price, confirm that the user's age is valid
– not negative and not more than 110. If it is not, give a message
and do not do the calculation. Also, the county name should not be
case sensitive – for example, Frederick, frederick, and FREDERICK
should all be acceptable.
Test your program with a variety of ages and counties to be sure
you have considered all the conditions. Here are some samples to
use.
Test run # County Age 1 Frederick 12 2 Frederick 72 3 Carroll 2 4
Howard 65 5 Washington 0 6 Frederick 5 7 Montgomery 6 8 Carroll 35
9 Frederick -15 10 Frederick 44 11 Howard 112 12 Cecil 13
Your program should be written with the future in mind. The Great
Frederick Fair might need to raise the basic prices or modify the
discounts in the future. (Hint: named constants, not hard-coded
literals!)
*These aren’t the real prices. The real system is much more
complicated. My favorite among the real ones is the Carload Special
Tuesday - $60 for everyone legally buckled in a vehicle, buses NOT
included.
Screenshot
----------------------------------------------------------------
Program
#Constants
BASE_PRICE=40
FREDERICK_PRICE=35
SENIOR_RATEPERCENTAGE=50
FREE_AGE=6
SENIOR_AGE=65
#Function to get the ticket rate
#Take age and country names as arguments
#return ticket rate
def calculateRate(age,county):
if(age<0 or age>110):
print('Invalid
age!!!')
elif(age<FREE_AGE):
return 0
elif(age>=SENIOR_AGE and
county.upper()=='FREDERICK'):
return
FREDERICK_PRICE*(SENIOR_RATEPERCENTAGE/100)
elif(age>=SENIOR_AGE):
return
BASE_PRICE*(SENIOR_RATEPERCENTAGE/100)
elif(county.upper()=='FREDERICK'):
return
FREDERICK_PRICE
else:
return BASE_PRICE
#Test cases
test1=calculateRate(12,'Frederick')
if(test1!=None):
print('Test1 ticket rate is $',test1)
test2=calculateRate(72,'Frederick')
if(test2!=None):
print('Test2 ticket rate is $',test2)
test3=calculateRate(2,'Carroll')
if(test3!=None):
print('Test3 ticket rate is $',test3)
test4=calculateRate(65,'Howard')
if(test4!=None):
print('Test4 ticket rate is $',test4)
test5=calculateRate(0,'Washington')
if(test5!=None):
print('Test5 ticket rate is $',test5)
test6=calculateRate(5,'Frederick')
if(test6!=None):
print('Test6 ticket rate is $',test6)
test7=calculateRate(6,'Montgomery')
if(test7!=None):
print('Test7 ticket rate is $',test7)
test8=calculateRate(35,'Carroll')
if(test8!=None):
print('Test8 ticket rate is $',test8)
test9=calculateRate(-15,'Frederick')
if(test9!=None):
print('Test9 ticket rate is $',test9)
test10=calculateRate(44,'Frederick')
if(test10!=None):
print('Test10 ticket rate is $',test10)
test11=calculateRate(112,'Howard')
if(test11!=None):
print('Test11 ticket rate is $',test11)
test12=calculateRate(13,'Cecil')
if(test12!=None):
print('Test12 ticket rate is $',test12)
-------------------------------------------------------------------
Output
Test1 ticket rate is $ 35
Test2 ticket rate is $ 17.5
Test3 ticket rate is $ 0
Test4 ticket rate is $ 20.0
Test5 ticket rate is $ 0
Test6 ticket rate is $ 0
Test7 ticket rate is $ 40
Test8 ticket rate is $ 40
Invalid age!!!
Test10 ticket rate is $ 35
Invalid age!!!
Test12 ticket rate is $ 40