Python Forum
My code doesn't work, can someone help me?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My code doesn't work, can someone help me?
#4
Hint: Use a function to split user-input and logic to get minimum and maximum.


def get_number():
    """
    Helper function to get one int from user input
    """
    while True:
        num_input = input("Enter a number: ")
        if num_input.lower() == "done": 
            return None
        try:
            return int(num_input)
        except ValueError:
            print('Invalid input')


def get_min_max():
    """
    Primitive implementation using a list with min and max.
    """
    numbers = []
    while True:
        number = get_number()
        if number is None:
            break
        numbers.append(number)
    return min(numbers), max(numbers)


def get_min_max_oneshoot():
    """
    Returns minimum and maximum of entered ints.
    The ints itself are not stored in a list.
    """
    first_run = True
    minimum = None
    maximum = None
    while True:
        number = get_number()
        if number is None:
            break
        if first_run:
            first_run = False
            minimum = number
            maximum = number
            continue
        minimum = min(number, minimum)
        maximum = max(number, maximum)
    return minimum, maximum


min_value_1, max_value_1 = get_min_max_oneshoot()
# min_value_2, max_value_2 = get_min_max()
The function get_min_max is the easier implementation.
get_min_max_oneshoot is like your implementation, but without storing any history values.
You get only min and max, all other values are not stored.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: My code doesn't work, can someone help me? - by DeaD_EyE - Dec-21-2020, 01:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 807 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 707 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 968 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 732 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Beginner: Code not work when longer list raiviscoding 2 836 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,849 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,465 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 905 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,279 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  cannot get code to work Led_Zeppelin 10 2,477 Jun-30-2022, 06:28 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020