[Python] CS50 Python Fuel Gauge

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm facing a problem with Fuel Gauge CS50's. My code's only passing through half of the requirements. Can someone help me with this one?

    https://cs50.harvard.edu/python/2022/psets/3/fuel/#demo

    My result while trying to check:

    :) fuel.py exists
    :) input of 3/4 yields output of 75% - OK
    :) input of 1/3 yields output of 33% - OK
    :) input of 2/3 yields output of 67% - OK
    :) input of 0/100 yields output of E - OK
    :) input of 1/100 yields output of E - OK
    :) input of 100/100 yields output of F - OK
    :) input of 99/100 yields output of F - OK
    **:( input of 100/0 results in reprompt - ERROR
    expected program to reject input, but it did not**
    :) input of 10/3 results in reprompt - OK
    **:( input of three/four results in reprompt - ERROR
    expected program to reject input, but it did not**
    **:( input of 1.5/4 results in reprompt - ERROR
    expected program to reject input, but it did not**
    **:( input of 3/5.5 results in reprompt - ERROR
    expected program to reject input, but it did not**
    **:( input of 5-10 results in reprompt - ERROR
    expected program to reject input, but it did not**


    My code:

    def main ():
    final_fuel = get_percent()
    percent_fuel = round(final_fuel * 100)
    if percent_fuel <= 1:
    print("E")
    elif percent_fuel >= 99:
    print("F")
    else:
    print(f"{percent_fuel}%")


    def get_percent():
    try:
    while True:
    fraction = input("Fuel Fraction: ")
    x, y = fraction.split('/')
    x = int(x)
    y = int(y)
    fuel = x/y
    if fuel <= 1:
    return fuel

    except (ValueError, ZeroDivisionError):
    pass


    main ()

    Continue reading...

Compartilhe esta Página