0

This is my assignment Toll roads have different fees based on the time of day. Here is the rate during different time periods:

12:00am to 7:29am -- $1.05
7:30am to 7:29pm -- $2.15
7:30pm to 11:59pm -- $1.10

Write a program that would calculate the tolls from the times a user enters.

Specifically, your program needs to:

let the user enter as many times as they want
    The program should keep asking the user for another time until the user enters "Done"
    The time should be in 12 hour format 

Links to an external site. but without spaces, e.g., "7:02am", "7:39am", "5:35pm", "7:20pm", "10:56pm"
Assume the user always enters a valid time

calculate the toll for each time the user enters

For example, the toll for "7:02am" is 1.05

store all the tolls in a list and print the list at the end of the program

I wrote a code and it looks like everything works until i got to return tolls. When I run it, it says return outside function and I don't know what that means

# Write a program that would calculate the tolls from the times a user enter
def calculate_toll(time):
   # Convert from 12 hours into 24 hours
    time_24h = convert_to_24h(time)

    if time_24h < 7*60 + 30:
        # Before 7:30 am
        return 1.05
    elif time_24h < 19*60 + 30:
        # Between 7:30 am and 7:30 pm
        return 2.15
    else:
        # After 7:30 pm
        return 1.10

def convert_to_24h(time):
   # Split into am and pm
    hour_str, minute_str = time[:-2].split(':')
     is_pm = time[-2:].lower() == 'pm'

    # Convert hour into integer
    hour = int(hour_str)

    if is_pm and hour != 12:
         hour += 12
    elif not is_pm and hour == 12:
        hour = 0

    # Convert back minutes from midnight
    return hour*60 + int(minute_str)

# Ask user for input until they type "Done"
# Assume all times entered are valid
def toll_times():
    tolls = []
while True:
    time_str = input("Enter a time or type 'Done': ")
    if time_str.lower() == "done":
        break
    tolls.append(toll)
return tolls

def main():
    tolls =toll_times
    toll = [calculate_toll(time_str)for time_str in times]
    print("The toll for", time_str, "is", toll)

if __name__ == '__main__':
    main()

Originally return tolls wasn't intented, so I indented it and it still didn't work

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • Welcome to Stack Overflow. Where the code says `while True:`, is that supposed to be inside the `toll_times` function, or outside? Does that give you a hint as to the actual problem with the indentation? – Karl Knechtel Mar 20 '23 at 22:42
  • `return tolls`, the ninth line up from the bottom of the program, is not inside a function. – John Gordon Mar 20 '23 at 22:43
  • Im not sure if it is supposed to be inside the function it just says return outside function – mya wood Mar 22 '23 at 02:01

0 Answers0