3 Replies - 313 Views - Last Post: 27 February 2014 - 09:41 PM Rate Topic: -----

#1 jocon128  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-February 14

Printing a list of number which the user inputs with a loop

Posted 27 February 2014 - 02:20 AM

How can I using lists, develop a python program that allows the user to enter as many numbers they wish into a list and then print them back to the user using a loop.
Is This A Good Question/Topic? 0
  • +

Replies To: Printing a list of number which the user inputs with a loop

#2 jocon128  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-February 14

Re: Printing a list of number which the user inputs with a loop

Posted 27 February 2014 - 03:16 AM

numberlist = []
for num in numberlist:
user = raw_input("Please enter a %s:" % num)
numberlist.append(user)
print numberlist
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Printing a list of number which the user inputs with a loop

Posted 27 February 2014 - 03:31 AM

Threads merged. Please keep posts on the same topic in the same thread.
Was This Post Helpful? 0
  • +
  • -

#4 maket  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-February 14

Re: Printing a list of number which the user inputs with a loop

Posted 27 February 2014 - 09:41 PM

If I understand correctly, you want to have the user input numbers and store them in an array to be printed.
The wording is slightly ambiguous but I'm going to assume from the code you provided that you want to print the array as a whole and not print each individual element.

You said the input was to be recieved in a for loop, since for loops (generally) have a set number of times to loop, you will have to ask the user the total number of elements they wish to input before the for loop.
listsize = int(raw_input("Enter the required length of list: "))


The for loop is used to fill the empty list, it loops asmany times as the user inputs into listsize, asking for a number on each loop and appending that number to the array.
Usually when making a list of numbers, those numbers are going to be used as integers and not strings, so I get the user input and convert it to integer format before appending to the list.
numberlist = []
for x in range(listsize):
    user = int(raw_input("Enter a number to add to the list: "))
    numberlist.append(user)


To print the list as a whole is simply
print numberlist



Again, I'm not quite sure that this is what you need, if it is not then please clarify and I'll do my best to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1