简体   繁体   中英

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

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.

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.

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 ^^

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM