python - Python 中的 For 循环(输出最小输入)

标签 python loops for-loop

好吧,我正在 Python 中练习 for 循环,我想知道如何让用户输入 10 个整数,然后它会输出最小的一个。例如,我会知道如何使用 while 循环来做到这一点:

Smallest = 0
count = 0
while count < 10:
    Number = int(input("Enter a number >> "))
    if Number < Smallest:
        Smallest = Number
    count = count + 1

print("{0} is the biggest value you have entered".format(Smallest))

但是我如何以 for 循环格式进行呢?这是我到目前为止所拥有的:

for i in range(10):
    Number = int(input("Enter a Number >> "))
    if Number < Smallest:
        Smallest = Number

print("{0} is the smallest value you have entered.".format(Smallest))

最佳答案

初始化您的 Smallest 变量,一切正常!

Smallest = int(input("Enter a Number >> "))
for i in range(9):
    Number = int(input("Enter a Number >> "))
    if Number < Smallest:
        Smallest = Number

print("{0} is the smallest value you have entered.".format(Smallest))

关于python - Python 中的 For 循环(输出最小输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39700100/

相关文章:

c++ - C++ 循环中 vector::size() 的性能问题

Java For循环终止条件: == vs <= vs >=

python - 在 python/django 应用程序中查找阻塞函数

python - 无法在 conda meta.yaml 文件中指定 pip 依赖项

java - 寻找最短路径(Dijkstra)的方法的实现陷入循环

loops - 我应该避免重置 "by hand"一个自动递增的循环变量吗?

javascript - 在 for 循环 javascript 中生成变量

python - Python : Error “TypeError: Can' t convert int to str implicitly”

python - 一个带有总和和平均值的简单 python 项目

jquery - 仅追加一次 Jquery