In: Computer Science
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help!
#Input Section
def main():
name=input("Please enter the customer's name:")
age=int(input("Enter age of the customer: "))
number_of_traffic_violations=int(input("Enter the number of traffic
violations: "))
if age <=15 and age >= 105:
print('Invalid Entry')
if number_of_traffic_violations <0:
print('Invalid Entry')
#Poccessing Section
def Insurance():
if age < 25 and number_of_tickets >= 4 and riskCode ==
1:
insurancePrice = 480
elif age >= 25 and number_of_tickets >= 4 and riskCode ==
1:
insurancePrice = 410
elif age < 25 and number_of_tickets == 3 and riskCode ==
2:
insurancePrice = 450
elif age >= 25 and number_of_tickets == 3 and riskCode ==
2:
insurancePrice = 390
elif age < 25 and number_of_tickets == 2 and riskCode ==
2:
inurancePrice = 405
elif age >= 25 and number_of_tickets == 2 and riskCode ==
2:
insurancePrice = 365
elif age < 25 and number_of_tickets == 1 and riskCode ==
3:
insurancePrice = 380
elif age >= 25 and number_of_tickets == 1 and riskCode ==
3:
insurancePrice = 315
elif age < 25 and number_of_tickets == 0 and riskCode ==
4:
insurancePrice = 325
elif age >= 25 and number_of_tickets == 0 and riskCode ==
4:
insurancePirce = 275
else:
insurancePrice = 0
def calc():
if number_of_traffic_violations >=4:
riskCode = 1
riskType = 'high'
elif number_of_traffic_violations == 3:
riskCode = 2
riskType = 'moderate'
elif number_of_traffic_violations == 2:
riskCode =3
riskType = 'moderate'
elif number_of_traffic_violations == 1:
riskCode = 4
riskType = 'low'
elif number_of_traffic_violations == 0:
riskCode = 5
riskType = 'no'
else:
riskCode = 0
riskType = 0
# OUTPUT SECTION
print(name,' as a ', riskType , ' risk driver, insurance Price will
cost $', format(insurancePrice, '.2f'))
main()
I have observed the Code
Note:Don't Directly Copy Paste from the Code Provided here. Because this Editor is Adding Some Error Element to it.
Paste it in a text file and type the code into your file.To get No errors.And Ignore Comments
1)the Main error is Indentation .But I haven't Seen You code Indented.
INDENTATION means making a group of statements into block by providing equal spacing before it.
2) Haven't defined number_of_tickets variable.
3)You have defined the methods but Not calling them .
Fixes:
1)I have made each function properly indented.
2)Inserted a input statement for taking no of tickets
3)Indented the code into three functions like main(),Insurance(),calc().And passed the Argument to each function.And Made each function return some values and stored it in some variable for further usage.
4) I moved the output Section into the Main() it Self .And from Main I called Both calac(),insurance()
Suggestion:
The If conditions are very complex So verify them Whether you inserted all the Condition or Not properly
Code in PYTHON 3:
def main():
name=input("Please enter the customer's name:")
age=int(input("Enter age of the customer: "))
number_of_traffic_violations=int(input("Enter the number of traffic
violations: "))
# line I added
number_of_tickets=int(input("Enter Number of Tickets"))
if age <=15 and age >= 105:
print('Invalid Entry')
if number_of_traffic_violations <0:
print('Invalid Entry')
else:
#lines I added
riskCode,riskType=calc(number_of_traffic_violations)
insurancePrice=Insurance(number_of_tickets,riskCode,age)
print(name,' as a ', riskType , ' risk driver, insurance Price will
cost $', format(insurancePrice, '.2f'))
#Poccessing Section
def Insurance(number_of_tickets,riskCode,age):
if age < 25 and number_of_tickets >= 4 and riskCode ==
1:
insurancePrice = 480
elif age >= 25 and number_of_tickets >= 4 and riskCode ==
1:
insurancePrice = 410
elif age < 25 and number_of_tickets == 3 and riskCode ==
2:
insurancePrice = 450
elif age >= 25 and number_of_tickets == 3 and riskCode ==
2:
insurancePrice = 390
elif age < 25 and number_of_tickets == 2 and riskCode == 2:
insurancePrice = 405
elif age >= 25 and number_of_tickets == 2 and riskCode ==
2:
insurancePrice = 365
elif age < 25 and number_of_tickets == 1 and riskCode ==
3:
insurancePrice = 380
elif age >= 25 and number_of_tickets == 1 and riskCode ==
3:
insurancePrice = 315
elif age < 25 and number_of_tickets == 0 and riskCode ==
4:
insurancePrice = 325
elif age >= 25 and number_of_tickets == 0 and riskCode ==
4:
insurancePrice = 275
else:
insurancePrice = 0
return insurancePrice
def calc(number_of_traffic_violations):
riskCode=0
if number_of_traffic_violations >=4:
riskCode = 1
riskType = 'high'
elif number_of_traffic_violations == 3:
riskCode = 2
riskType = 'moderate'
elif number_of_traffic_violations == 2:
riskCode =3
riskType = 'moderate'
elif number_of_traffic_violations == 1:
riskCode = 4
riskType = 'low'
elif number_of_traffic_violations == 0:
riskCode = 5
riskType = 'no'
else:
riskCode = 0
riskType = 0
return riskCode,riskType
main()
code ended here
HAve A great Day!