Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with faulty code.
#1
Why won't this code work? It doesn't run through as it's meant to and fails to set the variables to what they are meant to be! The first example is "carers" being undefined when it should be 2/3.

import time

def people_check(seniors):
    if seniors >= 10 and seniors <= 24:
        carers = 2
    elif seniors >= 25 and seniors <= 36:
        carers = 3
    else:
        if seniors < 10:
            print ("You can't have that few senior citizens on a trip, sorry! This program will now terminate.")
        elif seniors > 36:
            print ("You can't have that many senior citizens on a trip, sorry! This program will now terminate.")
        time.sleep(3)
        quit()

def money_calc(people):
    if people >= 12 and people <= 16:
        coach_price = 150.00
        meal_price = 14.00
        ticket_price = 21.00
    elif people >=17 and people <= 26:
        coach_price = 190.00
        meal_price = 13.50
        ticket_price = 20.00
    else:
        coach_price = 225.00
        meal_price = 13.00
        ticket_price = 19.00

def total_calc(coach_price, meal_price, ticket_price):
    meal_split = (meal_price * carers) / seniors
    ticket_split = (ticket_price * carers) / seniors
    meal_ind_total = meal_price + meal_split
    ticket_ind_total = ticket_price + ticket_split
    total = coach_price + (meal_price * people) + (ticket_price * people)
    ind_total = (coach_price / people) + meal_ind_total + ticket_ind_total
    total = round(total, 2)
    ind_total = round(ind_total, 2)

def full_code():
    seniors = int(input("How many senior citizens will be going on the trip?"))
    people_check(seniors)
    people = seniors + carers
    print ("There will be {0} carers and {1} people in total on the trip.".format(carers, people))
    money_calc(people)
    total_calc(coach_price, meal_price, ticket_price)
    print ("The total cost of the trip will be ${0} and the cost per senior citizen will be ${1}.".format(total, ind_total))

check = str(input("Would you like to do a calculation?"))

if check == "Yes" or check == "yes":
    full_code()
elif check =="No" or check == "no":
    quit()
else:
    print ("Sorry, that is not accepted, this program will now terminate.")
    time.sleep(3)
    quit()
Reply
#2
people_check function doesn't return the carers
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Redirection giving faulty results monstrup 5 2,389 Jul-19-2020, 07:11 AM
Last Post: Gribouillis
  My first program is a bit faulty. Please Help. Manjushri 3 2,149 Dec-06-2019, 01:12 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020