0

I'm taking an intro to python class online and the site is designed to auto-enter input() data into the program that you write to resolve various python logic problems.

Please see this page to see how the online class's tool uses input entries

For example the class randomly inputs the following:

30
centered
text
is
great
testing
is
great
for
python!
END

Obviously, I would have to convert the 30 to an int. How do I convert the rest into a usable list or array?

width = int(input())
lis = ['centered', 'text', 'is', 'great', 'END']
1
  • I'm not really sure what you're asking. You append an element to a list like name.append(element). And it seems you already know how to read input with input(). So what are you having trouble with? Commented Sep 4, 2013 at 19:07

3 Answers 3

1

It sounds like you want to call input in a loop. Here's one way to do it:

lst = []
s = input()
while s != 'END':
    lst.append(s)
    s = input()

There are other options for how to set up the condition on the loop, but I think this is the most straight forward. If the calculation for when to stop looping were more complicated, an alternative design might be to make the loop unconditional (with while True) and then break if the right conditions were met.

Sign up to request clarification or add additional context in comments.

Comments

0

You can create a list and read each string one by one, and add it to the list:

width=int(input())
lis=[]
tmp=''
while tmp!='END':
    tmp=input()       #receives a string, in python 3.0+
    lis.append(tmp)

Comments

0

The input() method they provide you will return each line of the user input as it's called. For example, the following function prints each line of the input by calling input throughout a loop

for line in range(6):
   print(input())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.