Python Forum
I can't figure out how to create variables in a while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I can't figure out how to create variables in a while loop
#1
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
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 872 Nov-14-2023, 08:46 AM
Last Post: perfringo
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,526 Nov-07-2023, 09:49 AM
Last Post: buran
Question How create programmatically variables ? SpongeB0B 6 1,217 Aug-19-2023, 05:10 AM
Last Post: SpongeB0B
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,430 Jul-27-2022, 08:50 PM
Last Post: rob101
  Create array of values from 2 variables paulo79 1 1,056 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  How to create 2 dimensional variables in Python? plumberpy 5 1,792 Mar-31-2022, 03:15 AM
Last Post: plumberpy
  how to use 3 variables python loop evilcode1 2 1,632 Nov-12-2021, 11:43 AM
Last Post: jamesaarr
  Create Dynamic For Loop quest 3 4,292 Apr-26-2021, 02:03 PM
Last Post: ibreeden
  Python Matplotlib: How do I save (in pdf) all the graphs I create in a loop? JaneTan 1 8,781 Feb-28-2021, 06:20 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020