O Levels 2210 Computer Science Pre-Release: Senior Citizens Trip Organizer Can anyone go over the code and suggest any edits and/or efficient solutions?

Attached below are the provided tasks and their solutions. The task is copyright of Cambridge International Examinations.

   

#©Nosher Zapoo 2017.
#Task 1 starts here.

number_of_citizens = int(input('Please Enter the number of citizens on the trip: '))

#Validation Check:

if number_of_citizens &gt; 2 and number_of_citizens &lt; 37:
    if number_of_citizens &gt; 24 :
        carer = 3
    else:
        carer = 2
else:
    print('Sorry the range of people is 10 - 36. Please Try again.')
    exit()

#Calculating the cost using economies of scale.

total_people = number_of_citizens + carer
if total_people &lt; 17:
    hire = 150
    cost_meal = 14*total_people
    cost_ticket = 21*total_people
elif total_people &lt; 27:
    hire = 190
    cost_meal = 13.5*total_people
    cost_ticket = 20*total_people

elif total_people &lt; 40:
    hire = 225
    cost_meal = 13*total_people
    cost_ticket = 19*total_people
else:
    exit()

#Working out the total cost
total_cost= hire + cost_meal + cost_ticket
avg_cost= total_cost / number_of_citizens
print('Number of Citizens = ',number_of_citizens)
print ('Total Cost = $', total_cost)
print ('Cost per Person = $', avg_cost)

#Task2 Starts!!!

#Creating Empty Lists to append as arrays.
names = []
amounts = []
total_amount  = 0

#Using an undefined loop
for x in range (0, total_people ):
    name = input('Name: ')
    amount = input ('Amount: ')
    names.append (name)
    amounts.append (amount)
    total_amount = total_amount + int(amount)

#Adding Spare people and their costs
if total_people &lt; 17:
    extra_people = 16 - total_people
elif total_people &lt; 27:
    extra_people = 26 - total_people
elif total_people &lt; 40:
    extra_people = 39 - total_people

extra_cost = extra_people*avg_cost
total_cost = total_cost + extra_cost

if extra_people &gt; 0:
    print ('There are', extra_people, 'seats available. Please enter the name &amp; amount of people joining on extra seats')
    updated_people = total_people + extra_people
    for x in range (total_people, updated_people):
        name = input('Name: ')
        amount = input ('Amount: ')
        names.append (name)
        amounts.append (amount)
        total_amount = total_amount + int(amount)

for x in range (0, updated_people):
    print (names[x])

#Task 3 Starts here.

profit = total_amount - total_cost 
if profit &gt; 0:
    print ('Profit gained $', profit)
elif profit == 0:
    break_even_value = total_cost
    print ('The Outing has broken even as the Total Cost is equal to the Total Amount.')
else:
    print ('The Outing is in loss of $', profit)
There are a few things to improve:

Task 1


the initial validation check is not correct. You are checking for a minimum number of people to be more than 2, while the error message says the minimum is 10: 


  Sorry the range of people is 10 - 36. Please Try again.

I would use an inclusive ranges for readability. Also, you can use the chained comparison:

if 10 &lt;= number_of_citizens &lt;= 36

you can use a short if/else form when defining carer:

carer = 3 if number_of_citizens &gt; 24 else 2

to avoid over-nesting things, I would handle the "negative case" first:

if not(10 &lt;= number_of_citizens &lt;= 36):
    print('Sorry the range of people is 10 - 36. Please Try again.')
    exit()

carer = 3 if number_of_citizens &gt; 24 else 2

don't hardcode the coefficient values for costs and hires, define them as constants, or better in a separate configuration file or module


Task 2

Some of the things from the "Task 1" can also be applied here as well.


when you have a throwaway (not used, but syntactically needed) variable, it is agreed to use _ (underscore) variable name
looks like you don't have to keep the amounts in a list and can simply calculate the total amount, while only collecting names into a list
the range() starts at 0 by default, you can avoid specifying 0: 

for _ in range(total_people):
     # ...



The definition step then might look like this:

names = []
total_amount  = 0

for _ in range(total_people):
    names.append(input('Name: '))
    total_amount += int(input('Amount: '))



when you print out the names, you can loop over the names in a list directly instead of looping over indexes:

for name in names:
    print(name)



Task 3


the break_even_value variable is set but never used


There were also some PEP8 violations in terms of code style, like missing spaces around operators or extra spaces after the function calls. Install a linter like flake8 or pylint to catch this kind of common violations.