'Need help finding the smallest number and ending the program with -1
This is what I got:
H = int(input("Enter a number or enter -1 to end)"))
smallest = H
counter=0
while(True):
number = int(input("Enter a number: "))
if(number == -1):
break
if(number < H):
smallest = number
print(number, "is the smallest")
What am I doing wrong?
Solution 1:[1]
It appears your question suffered some format issues but I get what you're asking. Really, you're asking the user to "Enter a number (or -1 to end):" repeatedly and finding the smallest.
With that in mind, I put the first time you prompt the user into the loop also. Keep in mind:
- 'None' is Python's way of saying not yet set.
- You have to test for 'None' first
- It helps you identify when you enter -1 right off the bat.
So here's a solution similar to yours:
number = None
smallest = None
while(True):
number = int(input("Enter a whole number (or -1 to end): "))
if(number == -1):
break
if smallest == None or number < smallest:
smallest = number
if smallest == None:
print ("No number entered.")
else:
print(smallest, "is the smallest")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | halfer |
