[Python] Coursera - Python for everybody - ex 5.2

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

  1. Stack

    Stack Membro Participativo

    For the following problem:
    Write a program that repeatedly prompts a user for integer numbers until the user enters done.
    Once 'done' is entered, print out the largest and smallest of the numbers.
    If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number.
    Input Cases:
    Enter 7, 2, bob, 10, and 4 and match the output below.

    My program is not correctly showing the ans. What is problem here?

    while True:
    inp = input("Enter number: ")

    if inp == "done":
    print("done")
    break
    try:
    inp_data = int(inp)
    except:
    print("Invalid input")
    if smallest is None:
    smallest = inp_data
    elif inp_data < smallest:
    smallest = inp_data
    elif inp_data > largest:
    largest = inp_data

    print("largest", largest)
    print("smallest", smallest)


    Output:
    Invalid input, Maximum is 10, Minimum is 2.
    ​

    [​IMG]

    Continue reading...

Compartilhe esta Página