I want to write a program which reads numbers from the user, until the user enters "0"
and then the program returns the lowest number in that string. I am having trouble sorting out the string.
This is my code so far:
while True:
number = []
number = input("Please enter a number ")
if number == "0":
break
number.sort()
x = number[0]
return x
CodePudding user response:
You overwrite your number at each step, this cannot work. You should keep track of the min value instead:
min_value = None
while True:
number = int(input("Please enter a number ")) # no type check here
if number == 0:
break
if number < min_value:
min_value = number
print(min_value)
CodePudding user response:
You can make a list of your entered numbers appending them and then finding the minimum value using the numpy
module. For example:
import numpy as np
while True:
number_list = []
number = input("Please enter a number ")
number_list.append(number)
if number == "0":
break
number_array = np.array(number_list)
minval = np.min(number_array[np.nonzero(number_array)])
So that you can find the minimum value of the array, excluding the "0" you entered at the end.