cuong tran
cuong tran

Reputation: 3

I do not understand this code

lines = []
while True:
    s = input()
    if s:  # i don't understand. # What does it have in the program?
      lines.append(s.upper())
    else:
      break
for sentence in lines:
    print(sentence)

I want to understand it. Please help me. Thank you so much

Upvotes: 0

Views: 136

Answers (5)

Teoretic
Teoretic

Reputation: 2533

First of all, you need to add missing indentations (as Colonbracket has mentioned)

lines = []
while True:
    s = input()
    if s:
        lines.append(s.upper())
    else:
        break

for sentence in lines:
    print(sentence)

You need to be careful with indentations in Python, since logic of Python program heavily depends on indentations.

For more info on this see "First Steps Towards Programming" section of Python tutorial

Secondly, based on what I understand about this code, its aim is to:

  1. Read lines from standard input (s = input()) until it gets an empty string from the user (break).
  2. Every line of this input is converted to an upper case (s.upper()) and stored in list data structure (lines.append)
  3. After user inputs an empty string, the program outputs all upper-cased strings to the standard output (for sentence in lines: print(sentence)) and then exits.

Upvotes: 4

BillyN
BillyN

Reputation: 185

lines = []
while True:
    s = input()
    if s: 
        enter code here`lines.append(s.upper())
    else:
        break

    for sentence in lines:
        print(sentence)

The part that you commented is equivalent to:

if s == True: 
    enter code here lines.append(s.upper())

Its simply a shorter method of typing it

Upvotes: 0

jakvah
jakvah

Reputation: 11

Your indentation is wrong. The "else" statement has to be inside the while loop, or you will get an infinite loop.

An empty string will be evaluated as false in python. Your program will ask the user for input and add it to a list as long as the input is not an empty string. If it is an empty string, it will break the while loop, and print whatever the user has entered before that.

Upvotes: 0

yogi
yogi

Reputation: 266

In python, input() method is used to get input from the user during runtime.

lines = []
while True:
    s = input()
    if s:  # It checks if the user has entered a string of length>0.
      lines.append(s.upper())
    else:
      break
for sentence in lines:
    print(sentence) 

By the way, I feel the code is not properly formatted. Basically, the code reads input from user and stores in a list as long as the user provides a valid string. Else it breaks the loop and prints all lines.

Upvotes: 1

Colonbracket
Colonbracket

Reputation: 33

I am not the most adept in Python, but I think your indentation is off?

   lines = []

    while True:
        s = input()
        if (s):  #pretty sure you're meant to bracket these maybe i wrong tho
            lines.append(s.upper())
        else:
            break
        for sentence in lines:
            print(sentence)

Also, I didn't think thats how For works in Pyhton but I may be wrong. Anyways this is a start sorry for not being super useful ^^

Upvotes: 0

Related Questions