HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

O Levels 2210 Computer Science Pre-Release: Senior Citizens Trip Organizer

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sciencecitizens2210prelevelscomputerreleaseseniortriporganizer

Problem

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 > 2 and number_of_citizens  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  0:
    print ('There are', extra_people, 'seats available. Please enter the name & 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 > 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)

Solution

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 <= number_of_citizens <= 36


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

carer = 3 if number_of_citizens > 24 else 2


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

if not(10  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.

Code Snippets

if 10 <= number_of_citizens <= 36
carer = 3 if number_of_citizens > 24 else 2
if not(10 <= number_of_citizens <= 36):
    print('Sorry the range of people is 10 - 36. Please Try again.')
    exit()

carer = 3 if number_of_citizens > 24 else 2
for _ in range(total_people):
     # ...
names = []
total_amount  = 0

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

Context

StackExchange Code Review Q#155339, answer score: 2

Revisions (0)

No revisions yet.