Python Forum

Full Version: I can't figure out how to create variables in a while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have written a program that finds the smallest/biggest of any 10 numbers you give, and I was wondering about how I could just take any amount of numbers and then find the smallest/biggest when the user inputs, for example "done". So if anyone could help me with my problem I'd really appreciate it. I also looked through the python documents but I wasn't sure what to look for so I couldn't find anything.

Here is the code I wrote if it helps in any way
 #Imports sleep from time module.
from time import sleep
#Gets the users name and then greets them.
name=str(input("Hello! What's your name? -->"))
print("Hey %s! Welcome to this program that finds the biggest or smallest of any 10 numbers you give!"%name)
#All 10 number inputs.
n1=int(input("Input the first number ->"))
sleep(0.1)
n2=int(input("Input the second number ->"))
sleep(0.1)
n3=int(input("Input the third number ->"))
sleep(0.1)
n4=int(input("Input the fourth number ->"))
sleep(0.1)
n5=int(input("Input the fifth number ->"))
sleep(0.1)
n6=int(input("Input the sixth number ->"))
sleep(0.1)
n7=int(input("Input the seventh number ->"))
sleep(0.1)
n8=int(input("Input the eighth number ->"))
sleep(0.1)
n9=int(input("Input the ninth number ->"))
sleep(0.1)
n10=int(input("Input the tenth number ->"))
sleep(0.3)
print("Okay, now that all the numbers are entered.")
chose=0
outCome=0
#Makes sure the smallest/biggest variable is correct and if not asks the user again.
while chose==0:
    choice = str(input("Do you want to find the smallest(s) or biggest(b)? ->"))
    sleep(0.6)
    if choice=="s":
        print("You you chose smallest!")
        chose=1
        break
    elif choice=="b":
        print("You chose biggest!")
        chose=1
        break
    else:
        print("Your choice was invalid, check your spelling!")
#The "smallest" decision.
if choice=="s":
    if n1 < n2:
        outCome=n1
    else:
        outCome=n2
    if n3 < outCome:
        outCome=n3
    if n4 < outCome:
        outCome=n4
    if n5 < outCome:
        outCome=n5
    if n6 < outCome:
        outCome=n6
    if n7 < outCome:
        outCome=n7
    if n8 < outCome:
        outCome=n8
    if n9 < outCome:
        outCome=n9
    if n10 < outCome:
        outCome=n10
#The "Biggest" decision.
if choice=="b":
    if n1 > n2:
        outCome=n1
    else:
        outCome=n2
    if n3 > outCome:
        outCome=n3
    if n4 > outCome:
        outCome=n4
    if n5 > outCome:
        outCome=n5
    if n6 > outCome:
        outCome=n6
    if n7 > outCome:
        outCome=n7
    if n8 > outCome:
        outCome=n8
    if n9 > outCome:
        outCome=n9
    if n10 > outCome:
        outCome=n10
sleep(0.6)
#Prints the correct text according to the users input.
if choice=="s":
    print("The smallest number is %s."%outCome)
    sleep(0.6)
    input('Press "Enter" to exit!')
if choice=="b":
    print("The biggest number is %s."%outCome)
    sleep(0.6)
    input('Press "Enter" to exit!')
If there isn't a way to achieve this, thanks anyway Big Grin
numbers = ['one', 'two', 'three']

for number in numbers:
    num = int(input('Enter the number {}: '.format(number)))
    # etc.
You can change it according to your need easy.