0

I'd like to write a python program that asks the user to input 7 numbers. This input should be converted into a list and display the individual numbers first, and then display the list as a whole. So my thought was i'd start with a for loop.

for a in range(1,8):
    number = int(input("please enter a number"))
    b = [a]
    print(b)

So it should look like this:

please enter number: 4
4
please enter number: 14
14
please enter number: 25
25

(repeat 4 more times)

And in the end:

Your list is: [4, 14, 25, ...]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
marv
  • 37
  • 1
  • 1
  • 3

6 Answers6

1
result = []

for a in range(1,8):
    number = int(input("please enter a number: "))
    result.append(number)

print(result)

A sample output is like:

please enter a number: 1
please enter a number: 2
please enter a number: 3
please enter a number: 4
please enter a number: 5
please enter a number: 6
please enter a number: 7
[1, 2, 3, 4, 5, 6, 7]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Daming Lu
  • 316
  • 1
  • 5
  • 21
0
Numbers = []

for a in range (1,8):
    number = int(input("Please enter a number:"))
    Numbers.append(number)
    print(number)
zondo
  • 19,901
  • 8
  • 44
  • 83
Joe
  • 11
0

Assuming that you don't need to display a list every time (see my comment):

b = []
for a in range(7):
    number = int(input("please enter a number: "))
    b.append(number)
    print(number)
print (b)
lmsteffan
  • 840
  • 4
  • 13
0

So, it sounds like you're fairly new to Python or otherwise unfamiliar with the built-in data structures. It's always obligatory to recommend the documentation, though, if you're new to computer programming, and not just Python's data structures, there are other resources that are valuable to learning, as well.

For the most complete answer to the question, which includes string formatting and escape sequences, this method will get the exact output.

def collect_user_input():
    list_of_numbers = []
    for x in range(7):
        number = int(input("Enter a Number: "))
        list_of_numbers.append(number)
        print("\nYour number was: %d\n" % number)
    print("Your list is: \n", list_of_numbers)

collect_user_input()
Jordon Birk
  • 480
  • 1
  • 9
  • 28
0

You can also get all numbers in one go:

nums = input("Please input space seperated numbers - as much as you need: ")
splitted = list(map(int, (x for x in nums.split() if x.isdigit()) ) )

print(splitted)

Output for input "5 4 55 6 asdf 923 7.23":

Please input space seperated numbers - as much as you need: 5 4 55 6 asdf 923 7.23
[5, 4, 55, 6, 923]

This drops any non-integer inputs being inputted automatically and converts the rest to integers.

You can limit the parsed numbers f.e. by:

splitted = splitted[:7]  # 7 numbers at most - might be less - you need to check with if

Doku:


Explanation for list(map(int, (x for x in nums.split() if x.isdigit()) ) ):

  • creates a list
    • from applying int()
      • to each element of split() of your input
        • only if the splitted part x is True if tested with isdigit()
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
-1
l1=[]  
for i in range(1,11):  
  inp=int(input("enter a number"))  
  l1.append(inp)  
print(l1)  
Luuklag
  • 3,897
  • 11
  • 38
  • 57
rena
  • 1
  • 1