Hello,
I need your help to get my Python program running please.
Thank you so much.

This is the assignment:

Highway Robbery!
Use skills learned in Units 1 - 4 and include:
• input functions, calculation functions and output functions
• passing variables between functions
• elif statement (if/else for App Inventor)

You have been tasked to construct a program that will prepare automobile liability insurance estimates for customers. The input consists of:
The name of the customer
The age of the customer
The number of traffic violations

Based on this information you will determine their status as a driver, following these guidelines:

Number of Violations Risk Code Risk Type
4 or More 1 High
3 2 Moderate
2 2 Moderate
1 3 Low
0 4 No

More than 4 tickets indicate a “Code 1” driver, which is High Risk. “Code 2” Drivers who have either 2 or 3 tickets, are considered “Moderate Risk” drivers. Any driver with one ticket is deemed “Code 3,” and considered “Low Risk.” Drivers without any tickets are considered “Code 4,” which is “No Risk.” Based upon this classification you can then give them the appropriate quote.

Pricing of Insurance:
Age Number of Tickets Risk Code Price
Under 25 4 or more 1 $480.00
25 or older 4 or more 1 $410.00
Under 25 3 2 $450.00
25 or older 3 2 $390.00
Under 25 2 2 $405.00
25 or older 2 2 $365.00
Under 25 1 3 $380.00
25 or older 1 3 $315.00
Under 25 0 4 $325.00
25 or older 0 4 $275.00

Sample output line follows (Items underlined are variable values):
“Customer Name, as a ---- risk driver, your insurance will cost -----.”

You are to account for invalid input. Drivers must be between the ages of 16 and 105. Number of traffic violations cannot be less than 0. Display only the message “Invalid Entry” in either case. You will need to submit this program in App Inventor and Python.

Here is my program:

# XXXX program gives the risk of a driver,and a insurance quote
# XXXXX constants
number_of_traffic_violations = 0
riskCode = 0
riskType = 0
age = 0
number_of_tickets = 0
insurancePrice = 0

# XXXXXXXX function
def getinput():
global number_of_traffic_violations
# XXX number_of_traffic_violations
name = int(input("Enter the name of the customer: "))
age = int(("Enter the age of the customer: "))
number_of_traffic_violations = int(input("Enter the number of traffic violations: ")
# XXXXXXXXX the risk code
def calRisk(type):
global riskType
global riskCode
if amount = 4:
riskCode = 1
riskType = high
elif amount = 3:
riskCode = 2
riskType = moderate
elif amount = 2:
riskCode = 3
riskType = moderate
eif amount = 1:
riskCode = 4
riskType = low
elif amount = 0:
riskCode = 5
riskType = no
else:
riskCode = 0
riskType = 0
# XXXXXXXXX the insurance price
def calInsurance(price):
global age
global numberoftickets
global riskCode
if age = <= 24 and number of tickets >= 4 and risk code = 1:
insurancePrice = 480
elif age = >= 25 and number of tickets >= 4 and risk code = 1:
insurancePrice = 410
elif age = <= 24 and number of tickets = 3 and risk code = 2:
insurancePrice = 450
elif age = >= 25 and number of tickets = 3 and risk code = 2:
insuranceprice = 390
elif age = <= 24 and number of tickets = 2 and risk code = 2:
insurancePrice = 405
elif age = >= 25 and number of tickets = 2 and risk code = 2:
insuransceprice = 365
elif age = <= 24 and number of tickets = 1 and risk code = 3:
insurancePrice = 380
elif age = >= 25 and number of tickets = 1 and risk code = 3:
insurancePrice = 315
elif age = <= 24 and number of tickets = 0 and risk code = 4:
insurancePrice = 325
elif age = >= 25 and number of tickets = 0 and risk code = 4:
insurancePrice = 275
else:
insurancePrice = 0
# XXXXXXXXX the risk type and show it
risk type = number of violations + risk code
print ("Risk Type:, ")
# XXXXXXXXX the insurance price and show it
insurancepice = age + number of tickets + risk code
print ("Insurance Price :$", format(insuranceprice, '.2f)
# XXX showPurchase function accepts riskType, insurancePrice as
# XXXXXXXXX and displays the amonunts
def showPurchase(riskType, insurancePrice):
print ("risk Type:, ")
print ("insurance Price :$", format(insurancePrice, '.2f)
getinput()
calRisk(type)
showpurchase(riskType, insurancePrice)

Recommended Answers

All 2 Replies

You have not used code tags, but even you have not indetion at all (which would come visible in quoted text after pushing reply button)!

For starters you can almost always use a list or dictionary instead of many if/elif statements. Also, globals are not necessary for this problem and point to a lack of understanding.

## replace of of these if/elif with a list of lists
"""   
if amount = 4:
riskCode = 1
riskType = high
elif amount = 3:
riskCode = 2
riskType = moderate
elif amount = 2:
riskCode = 3
riskType = moderate
eif amount = 1:
riskCode = 4
riskType = low
elif amount = 0:
riskCode = 5
riskType = no
else:
riskCode = 0
riskType = 0
if amount = 4:
riskCode = 1
riskType = high
elif amount = 3:
riskCode = 2
riskType = moderate
elif amount = 2:
riskCode = 3
riskType = moderate
eif amount = 1:
riskCode = 4
riskType = low
elif amount = 0:
riskCode = 5
riskType = no
else:
riskCode = 0
riskType = 0
"""

def calc_risk(amount):
    """ associate the offset of a list to the amount,
        i.e. amount=0 --> risk_code_type[0]
    """
    risk_code_type=[[5, "no"], [4, "low"], [3, "moderate"],
                    [2, "moderate"], [1, "high"]]
    risk_code=0     ## defaults for amount < 0 or amount > 4
    risk_type=0
    if 0 <= amount <= 4:
        code_type = risk_code_type[amount]
        ## print for testing
        print "sub-list for amount=", amount, code_type        
        risk_code=code_type[0]
        risk_type=code_type[1]

    return risk_code, risk_type

for amount in [-1, 0, 1, 2, 3, 4, 5]:
    rcode, rtype = calc_risk(amount)
    print "amount=%d, risk code=%d, risk_type=%s" % (amount, rcode, rtype)
#
#---------- you can also use 2 lists --------------------------------------
print "-"*50

risk_code=[5, 4, 3, 2, 1]
risk_type=["no", "low", "moderate", "moderate", "high"]
amount=2
print "risk code =", risk_code[amount]
print "risk type =", risk_type[amount]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.