PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 13, 2022

[FIXED] How to input numbers of a list individually from user input?

 May 13, 2022     append, input, python     No comments   

Issue

I need make program that asks, how many numbers I want to add list example 5, then it asks individually those numbers and last prints out list in that order. I otherwise got to work but don't know how to do that part when it asks individually those numbers.

This is what program should work.

How many numbers you want to add list: 5

Number 1: 76
Number 2: 9
Number 3: 32
Number 4: 77
Number 5: 3

List:
[76, 9, 32, 77, 3]

And here is my code:

amount = int(input("How many numbers do you want to add to the list? "))
numbers = []
numbers.append()
print(numbers)

Solution

Try this.

I am using f-strings here. This only works in Python 3.6+.

n = int(input('How many numbers do you want to add to the list? '))
lst = []
for i in range(1,n+1):
    temp = int(input(f'Number {i}: '))
    lst.append(temp)

print(f'List:\n{lst}')

Output:

How many numbers do you want to add to the list? 5

Number 1: 67
Number 2: 6
Number 3: 45
Number 4: 9
Number 5: 1

List:
[67, 6, 45, 9, 1]


Answered By - Ram
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing