3

I am new here and a total beginner of python so please help me. I have done the following to creat a program that tells the user if number entered is Up, Down or the same from the first number entered. my question is, how can I create a loop for it so they user can enter as many numbers then end the process when 0 is entered?


print ('Please enter your first number')
firstNumber = input()

print('Enter the next number(0 to finish)')
nextNumber=input()

if firstNumber<nextNumber:
    print ('Up')

if firstNumber>nextNumber:
    print ('Down')

if firstNumber==nextNumber:
    print ('Same')

Thank you

AChampion
  • 29,683
  • 4
  • 59
  • 75
Kaka Pin
  • 81
  • 1
  • 9

2 Answers2

2

Translating from english to python, what you want is

while nextNumber != 0:
    ...keep looping...

So, without changing your code too much, you can simply do:

print ('Please enter your first number')
firstNumber = input()

print('Enter the next number(0 to finish)')
nextNumber=input()

while nextNumber != 0:

    if firstNumber<nextNumber:
        print ('Up')

    if firstNumber>nextNumber:
        print ('Down')

    if firstNumber==nextNumber:
        print ('Same')

    # turn the nextNumber into the firstNumber
    firstNumber = nextNumber

    print('Enter the next number(0 to finish)')
    nextNumber=input()

There are also a few things you could improve. For example, you dont need to use the print statement before the input. You can simply do input('Please enter your first number'). Also, you could be using if-else instead of only if.

Here is an example of how it would look like:

firstNumber = input('Please enter your first number')

nextNumber=input('Enter the next number(0 to finish)')

while nextNumber != 0:

    if firstNumber<nextNumber:
        print ('Up')

    elif firstNumber>nextNumber:
        print ('Down')

    elif firstNumber==nextNumber:
        print ('Same')

    # turn the nextNumber into the firstNumber
    firstNumber = nextNumber

    nextNumber=input('Enter the next number(0 to finish)')

Now, you say you want to collect all the Ups and Downs and print them at the end. You can simply create a list and append the value to that list. Like so:

ups_and_downs = []

firstNumber = input('Please enter your first number')

nextNumber=input('Enter the next number(0 to finish)')

while nextNumber != 0:

    if firstNumber<nextNumber:
        ups_and_downs.append('Up')
        print ('Up')

    elif firstNumber>nextNumber:
        ups_and_downs.append('Down')
        print ('Down')

    elif firstNumber==nextNumber:
        ups_and_downs.append('Same')
        print ('Same')

    # turn the nextNumber into the firstNumber
    firstNumber = nextNumber

    nextNumber=input('Enter the next number(0 to finish)')

print(ups_and_downs)
tpvasconcelos
  • 671
  • 7
  • 19
  • 1
    Thank you! can you please tell me how, after the user enters all the numbers then 0 to finish, the result Up, Up, Down .... is displayed all together at the end? – Kaka Pin May 03 '17 at 13:58
1
nextNumber = 1 #define 
while nextNumber != 0: #as long as its not 0, run loop
  print ('Please enter your first number')
  firstNumber = int(input()) #python treats input as str so cast to int

  print('Enter the next number(0 to finish)')#cast str to int
  nextNumber= int(input())

  if firstNumber<nextNumber:
      print ('Up')

  if firstNumber>nextNumber:
      print ('Down')

  if firstNumber==nextNumber:
      print ('Same')

Output:

Please enter your first number
 4
Enter the next number(0 to finish)
 6
Up
Please enter your first number
 7
Enter the next number(0 to finish)
 8
Up
Please enter your first number
 5
Enter the next number(0 to finish)
 4
Down
Please enter your first number
 8
Enter the next number(0 to finish)
 0
Down
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
  • 1
    Thank you very much, ... may I ask if there is any way to collect those outcomes in the end? i mean after the user enters all the numbers then 0 to end, the result Up, Up, Down .... is displayed all together? – Kaka Pin May 03 '17 at 13:52
  • Yes, use a list to store each input. Look in google on how to use a list. – Taufiq Rahman May 03 '17 at 13:54