[Python] Attempting to modify previous code to include a function, very confused

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am very new to python, and I am taking a course in it. One of my homework assignments instructs us to modify code from a previous assignment (which was to write a code that would take input of hour, minute, second, and AM/PM and convert from standard time to military time) and update it to "write a function that accepts the 12 hour time and AM or PM. The function returns the 24 hour time as a string." We are also not allowed to use python's built-in conversion for this. I've attached both the previous project and what I have so far, and I am admittedly confused about what to do because we haven't covered anything like this in class previously.

    hr = int(input("Enter the time in hours:"))
    mins = int(input("Enter the time in minutes:"))
    sec = int(input("Enter the time in seconds:"))
    apm = str(input("Enter either AM or PM:"))

    def function(hr,mins,sec,apm):
    print("12 HR",hr,":",mins,":",sec,":",apm)
    if apm == "AM" and hr == 12:
    hr = 0
    elif apm == "PM" and hr != 12:
    hr += 12
    print("24 HR",hr,":",mins,":",sec," ",apm)


    The only difference right now from the original assignment that I wrote is that I added the def function line. We get a hint that the main program should accept the input values, which will be passed to the function, the function will return the converted time as a string, and the main program will print that.

    Continue reading...

Compartilhe esta Página