1

Okay so I am practicing for loops in Python and I was wondering how could I make a user input 10 intergers then it would output the smallest one. I would know how to do this with a while loop for example:

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

But how do I do it in a for loop format? Here's what I have so far:

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))
2
  • 2
    min(int(input("Enter a Number >> ")) for i in range(10)) Commented Sep 26, 2016 at 10:17
  • @Chris_Rands you should definitely give that as an answer instead of a comment, cause it looks really slick :D Commented Sep 26, 2016 at 10:20

5 Answers 5

3

Initialize your Smallest variable and all will works!

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))
Sign up to request clarification or add additional context in comments.

Comments

2

What you have there, other than the fact you should initialise Smallest, is equivalent to the solution using your while statement.

So, assuming the while variant is considered correct, the for variant you have will suffice.

The reason I qualify it is because initialising Smallest to zero is actually the wrong thing to do. If your ten numbers are all greater than twenty (for example), Smallest will remain at zero, giving you the wrong answer.

One way to fix this (in both variants) is to change your if statement to:

if i == 0 or Number < Smallest:

which will set Smallest first time through the loop regardless (though it should be count in the while variant since that's using a different loop control variable).


Of course, as you learn more and more about the language, you'll come across the concept of things that are more "Pythonic" than others. An example of this is the rather more succinct:

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

which removes the need totally for an explicit check-and-replace strategy (that's still done, but under the covers of the min function).

2 Comments

I want to use a for loop though?
@Sam, I'm telling you to use the for loop, I'll clarify.
1

If you want to get the smallest of the input numbers, you need to start with a minimum a bit bigger than 0... and that is a problem you have with your while loop. It will only work if the user inputs at least one negative number, else it will return 0.

Here is what I suggest:

smallest = float("inf")
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))

Note that I did not capitalize the variables, because to me it makes them look like classes. ^^

1 Comment

A class is something used in Object Oriented Programming. If you do not know what it is, don't worry. You do not need to use OOP in Python.
0

Since you are finding the Smallest values out of the input values from the user it is good practice to initially initialize the Smallest variable as maximum possible integer as below.

import sys
Smallest = sys.maxint

Then the rest of your loop will work as properly as it is.

3 Comments

That's a good answer for Python 2 but the bracketed print in the question tends to indicate Python 3. There, maxint no longer exists, nor does the arbitrary limit on what an int can hold. It might be worth pointing that out.
yeah.. thanks for pointing out. I didn't noticed the brackets. In this case sys.maxsize can also be used in its place.
Almost: maxsize will give you the "natural" limit (such as 2^64 - 1) but the removal of arbitrary limits on integer types means you can have values greater than this - try import sys, x = sys.maxsize, print(x), print(x+1) to see this problem. In other words, the user entering all values greater than maxsize will not work so well, since the result will be maxsize.
0

try this

b = []

for i in range(9):

    a = int(input("Enter a Number >> "))
    b.append(a)


print(min(b))

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has existing answers. It would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.