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?
#1
my code:

largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done" : 
        break
    try:
        num1 = int(num)
    except ValueError:
        print('Invalid input')
        continue
for number1 in num:
    if largest is None:
        largest = number1
    elif largest > number1:
        largest = number1
print("Maximum is", largest)
for number2 in num:
    if smallest is None:
        smallest = number2
    elif smallest < number2:
        smallest = number2
print("Minimum is", smallest)
My desired output would be if I used 2,8,9,python,4,10 would be:

Invalid input
Maximum is 10
Minimum is 2

But my output shows:

Invalid input
Maximum is d ← Mismatch
Minimum is o ← Mismatch

d and o are from word done Wall
buran write Dec-21-2020, 11:23 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
You are using num for the input, then expect that num will end up being the list that you can cycle over, and you also never append the integer to the list.
Minor changes, try:
largest = None
smallest = None
num = []
while True:
    num_input = input("Enter a number: ")
    if num_input == "done" : 
        break
    try:
        num1 = int(num_input)
        num.append(num1)
    except ValueError:
        print('Invalid input')
        continue
for number1 in num:
    if largest is None:
        largest = number1
    elif largest > number1:
        largest = number1
print("Maximum is", largest)
for number2 in num:
    if smallest is None:
        smallest = number2
    elif smallest < number2:
        smallest = number2
print("Minimum is", smallest)
Output:
Enter a number: 2 Enter a number: 8 Enter a number: 9 Enter a number: python Invalid input Enter a number: 4 Enter a number: 10 Enter a number: done Maximum is 2 Minimum is 10
Reply
#3
Oh, and btw - you reverse minimum and maximum as shown in results above.
Reply
#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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Dec-21-2020, 01:09 PM)jefsummers Wrote: Oh, and btw - you reverse minimum and maximum as shown in results above.

Thank you for your help, my head almost exploded from this one. Smile
Reply
#6
(Dec-21-2020, 01:51 PM)DeaD_EyE Wrote: 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.

Thank you for the help, I appreciate it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Psycopg2 doesn't work with python2 MedianykEugene 3 332 Aug-10-2021, 07:00 AM
Last Post: ndc85430
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 868 Jul-17-2021, 04:19 PM
Last Post: bill_z
  string function doesn't work in script ClockPillow 3 444 Jul-13-2021, 02:47 PM
Last Post: deanhystad
Photo Windows 10 PowerShell doesn't work Amy 3 1,347 Apr-27-2021, 01:33 PM
Last Post: jefsummers
  Why doesn't this print statement work? stylingpat 10 1,298 Mar-23-2021, 07:54 PM
Last Post: buran
  keyboard module doesn't work in the microsoft version terminal of python. username 1 691 Feb-25-2021, 05:19 PM
Last Post: Larz60+
  pygame music doesn't work CompleteNewb 1 1,897 Jan-30-2021, 09:25 AM
Last Post: Tyrel
  I have two Same Code but One of them Doesnt Work beginner721 6 775 Jan-22-2021, 10:56 PM
Last Post: beginner721
  Curses script doesn't work wavic 1 874 Jan-08-2021, 09:11 PM
Last Post: wavic
  If, elif, else doesn't work well Vidar567 5 1,042 Nov-21-2020, 06:25 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

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