Inputng data into arrays

Hello all, I´m new to Python and I´m having difficulty with a problem. I´m posting the problem below. Then, below that what I´m unsure of and what I think might be the 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. Enter 7, 2, bob, 10, and 4 and match the output below.



while True:
    num = 0
    val = []
    sval = input("Enter a number: ")
    try:
        fval = float(sval)
        val.append(fval)
    except:
        print("Invalid Input")
        continue
  

    smallest = None 
    for fval in val:
        if smallest is None:
            smallest = val
        elif val < smallest:
            s = val
    largest = None
    for value in val:
        if largest is None:
            largest = value
        elif value > largest:
            l = value
    print("Maximum is", l)
    print("Minimum is", s)

I am unsure that my manner of creating an array to store the value accumulated from the user´s inputs are correct. I know that my variable l and s are not valid because of the cmd prompt´s comments; yet, I don´t know how else to pass the values into the string function.

I am applying your advice to my algorithm (I think), but the problem is I can´t connect the user input section of the algorithm to the processing of smallest and largest numbers.

I´ve tried using break but I keep getting “Invalid Input” (a failsafe for nonnumerical inputs) and I am unable to break out of part of the input.

What am I missing?

(Code is below)

while True:
    small = 0
    large = 0
    sval = input('Enter a number: ')

    try:
        fval = float(sval)

    except:
        print("Invalid Input")
        continue
            
    if sval == 'done':
        break
    small = fval
    smallest = None 
    if smallest is None:
        smallest = small
    elif small < smallest:
       print("Minimum is", small)
    largest = None
    large = fval
    if largest is None:
        largest = large
    elif large > largest:
        print('Maximum is', large)

Okay. I have system for storing the value if it meets the standard of bigger or smaller than the currently saved value.
I think I´ve got everything set up nicely; HOWEVER, I continue to output “Invalid Input” after I enter my first number and I don´t know why. The only time “Invalid Input” should output is if the user inputs a non numeral. It output this every time with the exception of my break statement
Please help

#global values
largest = None
smallest = None


while True:
#incorporate user input in algorithm & provide option to exit algorithm
    sval = input("Enter a number: ")
    if sval == "Done":
        break
 
#transform string to float, state conditions for determining largest and smallest number among user inputs
    try:
        sval = float(sval)
        if largest < sval: 
            largest = sval
        if smallest > sval:
            smallest = sval
    #if input is not float then output "Invalid Input" and continue with alogrithm
    except:
        print("Invalid Input")
        continue
#output smallest and largest integers       
    print("Minimum is", smallest)
    print("Maximum is", largest)

Well, I assumed that if
largest = None
and
sval = 5
If
None < 5

Then, largest = sval
would assign the value of sval to largest

largest = 5

Am I mislead in my understanding of the = operator?

My problem now is that my break command is the result for both my Maximum and Minimum values. I´ve tried putting the break command with try and except as well as with my conditional statements. It returns “Done” for my Max and Min values. I´m researching break and I can´t find an answer. I don´t think I´m misunderstanding the function of it…but I must be. Please help. (Code is below)

#global values
largest = None
smallest = None
sval = 0
    
while True:
#incorporate user input in algorithm 
    sval = input("Enter a number: ")


#transform string to float, state conditions for determining largest and smallest number among user inputs
# & provide option to exit algorithm

    try: 
        sval = int(sval)
    except:
        print("Invalid Input")
    
    if sval == "Done":
        break

#output smallest and largest integers
if largest is None: largest = sval
elif eval > largest: largest = sval  

if smallest is None: smallest = sval
elif eval < smallest: smallest = sval

print("Maximum is", largest)
print("Minimum is", smallest)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.