# 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.

Why do you need a list (Python does not have arrays)?

Why not just create a variable that stores the running sum of valid numbers?

Also, why not just keep track of the smallest and largest by making applicable comparisons to existing smallest and largest numbers (tracked globally)? That way you do not have to iterate through a list each time.

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)
``````

If you keep resetting these variables to `0` each iteration of the `while` loop, how are you ever going to know what the previous values of them are to make the correct comparisions?

Also, you do not want to print the min and max until after the while loop has completed.

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

``````#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)
``````

Let’s say that `largest` is `5` and `sval` is `6`, don’t you want `largest` to be assigned the value `6`? Currently, this logic will not allow that to happen.

Also, what happens if you try to make the following comparision?

``````None < 5
``````

What happens?

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?

You can not compare `None` to `5` as they are of different types.

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)
``````

These comparisons should be made within the while loop.