如何查找最小表单输入



我试着找到最小值和我的代码不给我min,我错过了什么?

def hahaha(number):
lista=[]
while len(lista)<user:
input_from_user=int(input())
lista=lista+[input_from_user]
for i in lista:
b=lista[0]
if b<i:
i=b
return b

user=int(input("how many students"))哈哈哈(用户)

我认为这就是你想要达到的目标。该函数按预定义的次数查询用户,并返回用户输入的最小值。为了保持简单,我没有添加任何错误捕获功能。

import math
def find_min_input(num_inputs):
min_input = math.inf #initialize the minimum to infinity
for i in range(num_inputs):
input_from_user = int(input(f'You have {num_inputs - i} numbers left to enter.nEnter a number: '))
if input_from_user < min_input:
min_input = input_from_user #update the minimum
print(f'The minimum of the numbers you entered is: {min_input}')
#sample run:
find_min_input(3)
You have 3 numbers left to enter.
Enter a number: 5
You have 2 numbers left to enter.
Enter a number: 12
You have 1 numbers left to enter.
Enter a number: 8
The minimum of the numbers you entered is: 5

试试这个for循环:

# pick the first element as the smallest
smallest = lista[0]
# iterate over each remaining item in the list
for number in lista[1:]:
# if this item is smaller than the smallest so far,
# pick it as the new smallest number
if number < smallest:
smallest = number
print("The smallest item is", smallest)

最新更新