how to print all the output at the end of the while loop in python

python while loop multiple conditions
while true python
while loop example
python while loop break
python while loop exercises
while loop python example
while loop practice problems python
python while loop user input

I already solved this with list.append() function however my instructor told me to just use the basic python functions. Here is my code:

    a = 0
    b = 0
    s = 0
    x = str(s)

    print ('Enter the first number: ', end = '')
    c = input()
    a = int(c)
    finished = False
    while not finished:
        print ('Enter the next number (0 to finish): ', end ='')
        n = input()
        b = int(n)
        if b != 0:
            if b == a: 
                x = ('Same')
            elif b > a:
                x = ('Up')
            elif b < a:
                x = ('Down')
            a = b
            s = x
        else:
            finished = True

    print (str(x))

I am aiming to print (e.g. Up Down Up Down Same in comparing the input integers) in one line at the end of the while loop. Let me know how can I improve my code. Thank you very much

Use string concatenation to get the result you want without using a list:

http://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python

I'll give you two hints on how to do this for your program:

  1. Initialize x as an empty string by replacing

    x=str(s)
    

    with

    x=""
    

    There is no need for it to begin as the string "0", which str(s) does since s is 0.

  2. Instead of saying

    x=('SAME')
    x=('UP')
    x=('DOWN')
    

try saying

    x=x+'SAME'
    x=x+'UP'
    x=x+'DOWN'

I removed the parentheses because they are not necessary.

As for style, it is good practice to name your variables as useful things instead of just letters. Last staement in an if/else chain that covers all bases should just be else. Best of luck to you sir

Python "while" Loops (Indefinite Iteration) – Real Python, The while Loop; The Python break and continue Statements; The else Clause; Infinite Loops Inside the loop body on line 3, n is decremented by 1 to 4 , and then printed. If it's false to start with, the loop body will never be executed at all: Running break.py from a command-line interpreter produces the following output:. Python allows an optional else clause at the end of a while loop. This is a unique feature of Python, not found in most other programming languages. This is a unique feature of Python, not found in most other programming languages.

Not sure what result you're looking for, but perhaps this works:

a = 0
b = 99
result = ""

a = int(input('Enter the first number: '))

while b != 0:
    b = int(input('Enter the next number (0 to finish): '))

    if b == a:
        result += ' Same'
    elif b > a:
        result += ' Up'
    elif b < a:
        result += ' Down'
    a = b

print(result.strip())

Output:

Enter the first number: 12
Enter the next number (0 to finish): 12
Enter the next number (0 to finish): 12
Enter the next number (0 to finish): 1
Enter the next number (0 to finish): 1
Enter the next number (0 to finish): 5
Enter the next number (0 to finish): 0
Same Same Down Same Up Down

Python For & While Loops: Enumerate, Break, Continue Statement, In this tutorial, you will learn For Loop, While Loop, Break, Continue block once​, It executes the code block multiple times until a certain condition is met. loops # x=0 #define a while loop while(x <4): print(x) x = x+1. Output The while loop tells the computer to do something as long as the condition is met. Its construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. a = 0 while a < 10: a = a + 1 print a. While Loop Example.

You can simply initialize x with an empty string and keep concatenating to it.

a = 0
b = 0
s = 0
x = ''

print('Enter the first number: ', end='')
c = input()
a = int(c)
finished = False
while not finished:
    print('Enter the next number (0 to finish): ', end='')
    n = input()
    b = int(n)
    if b != 0:
        if b == a:
            x += 'Same\n'
        elif b > a:
            x += 'Up\n'
        elif b < a:
            x += 'Down\n'
        a = b
        s = x
    else:
        finished = True

print(str(x))

While loop - Learn Python 3, For example, the following program fragment prints the squares of all integers 1. 2. 3. 4. 5. 6. i = 1. while i <= 10: print(i). i += 1. else: print('Loop ended, i =', i). Python’s print () function comes with a parameter called ‘end’. By default, the value of this parameter is ‘ ’, i.e. the new line character. You can end a print statement with any character/string using this parameter. # This Python program must be run with. # Python 3 as it won't work with 2.7. # ends the output with a <space>

Python Tutorial: While Loops, Chapter on loops with simple and practical examples using while loops in Python​. The standard output is usually the terminal or console where the script had while 1: c = sys.stdin.read(1) text = text + c if c == '\n': break print("Input: %s" % text) Descriptors · Inheritance · Multiple Inheritance · Multiple Inheritance Example  The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop. In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements. Flow Diagram

Python while loop, Python while loop: Loops are used to repeatedly execute block of program The condition is checked every time at the beginning of the loop and the Output: 0 1 2 3 4. One thing we should remember that a while loop x = 0; s = 0 while (x < 10): s = s + x x = x + 1 else : print('The sum of first 9 integers : ',s). In this step-by-step tutorial, you'll learn about the print() function in Python and discover some of its lesser-known features. Avoid common mistakes, take your "hello world" to the next level, and know when to use a better alternative.

All You Need to Know About the Python While Loop, The general syntax for a Python while loop statement is: loop_value + 1 print ('​Demonstration of a simple Python while loop program is The last 4 lines of the output will be displayed when you manually quit the program. You only had output in place of the "inner" putlog, hence you get 6 rows of output. As Art explained, variable X is increased one more time but the code inside your loop does not execute as the loop as now exceeded its end condition.

Comments
  • It would be helpful to show your version using list.append(). Did your instructor give any hint what they want? list.append() doesn't seem that exotic.
  • a = 0 b = 0 demands = [] print('Enter the first number: ' , end = ' ') c = input() a = int(c) finished = False while not finished: print('Enter the next number, 0 to finish: ' , end = ' ') n = input() b = int(n) if b != 0: if b == a: demands.append ('Same.') elif b > a: demands.append('Up.') elif b < a: demands.append('Down.') a = b else: finished = True print (demands)
  • I'm suggesting editing your question to include this information, it seems intrinsic to what you're asking.
  • List is the most basic data structure in Python!!!, then just concatenate your every output in a string, and show that summary string at the end