Back to Freecodecamp

Challenge 228: Movie Night

curriculum/challenges/english/blocks/daily-coding-challenges-python/69a890af247de743333bd4d1.md

latest3.0 KB
Original Source

--description--

Given a string for the day of the week, another string for a showtime, and an integer number of tickets, return the total cost of the movie tickets for that showing.

The given day will be one of:

  • "Monday"
  • "Tuesday"
  • "Wednesday"
  • "Thursday"
  • "Friday"
  • "Saturday"
  • "Sunday"

The showtime will be given in the format "H:MMam" or "H:MMpm". For example "10:00am" or "10:00pm".

Return the total cost in the format "$D.CC" using these rules:

  • Weekend (Friday - Sunday): $12.00 per ticket.
  • Weekday (Monday - Thursday): $10.00 per ticket.
  • Matinee (before 5:00pm): subtract $2.00 per ticket (except on Tuesdays).
  • Tuesdays: all tickets are $5.00 each.

--hints--

get_movie_night_cost("Saturday", "10:00pm", 1) should return "$12.00".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_movie_night_cost("Saturday", "10:00pm", 1), "$12.00")`)
}})

get_movie_night_cost("Sunday", "10:00am", 1) should return "$10.00".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_movie_night_cost("Sunday", "10:00am", 1), "$10.00")`)
}})

get_movie_night_cost("Tuesday", "7:20pm", 2) should return "$10.00".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_movie_night_cost("Tuesday", "7:20pm", 2), "$10.00")`)
}})

get_movie_night_cost("Wednesday", "5:40pm", 3) should return "$30.00".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_movie_night_cost("Wednesday", "5:40pm", 3), "$30.00")`)
}})

get_movie_night_cost("Monday", "11:50am", 4) should return "$32.00".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_movie_night_cost("Monday", "11:50am", 4), "$32.00")`)
}})

get_movie_night_cost("Friday", "4:30pm", 5) should return "$50.00".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_movie_night_cost("Friday", "4:30pm", 5), "$50.00")`)
}})

get_movie_night_cost("Tuesday", "11:30am", 1) should return "$5.00".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_movie_night_cost("Tuesday", "11:30am", 1), "$5.00")`)
}})

--seed--

--seed-contents--

py
def get_movie_night_cost(day, showtime, number_of_tickets):

    return day

--solutions--

py
def get_movie_night_cost(day, showtime, number_of_tickets):
    if day == "Tuesday":
        price = 5
    else:
        if day in ["Friday", "Saturday", "Sunday"]:
            price = 12
        else:
            price = 10

        hour = int(showtime.split(":")[0])
        period = showtime[-2:]

        hour24 = hour

        if period == "pm" and hour != 12:
            hour24 += 12
        if period == "am" and hour == 12:
            hour24 = 0

        if hour24 < 17:
            price -= 2

    total = price * number_of_tickets

    return f"${total:.2f}"