-2

I guess I could be able to handle the rest of the project but not on how to start the program. My problem scenario is, I will ask the user to input a random number. for example, he input 3. I should create then a program that iterates 3 times, while at the same time in between, asking the user some input. the idea is shown below:

Number 1:
    Enter your number:
    Enter your age:
Number 2:
    Enter your number:
    Enter your age:
Number 3:
    Enter your number:
    Enter your age:

In the end of my program I will need to do calculation, for example, which number item is the oldest? is it number1, number2, or number3...I think I could be able to handle that, My problem is how can store each Number item in one variable, with all the iterations going on....Im super confused. your help is much appreciated. thank you

  • 3
    Welcome to the site! What have you tried so far? Please look into the https://stackoverflow.com/help/how-to-ask. If you search I am fairly certain you can find help for your overall question by breaking it into smaller pieces. For example: How do I get user input in python 3? and how do I get user input in a loop in python 3? – ak_slick Aug 24 '18 at 13:08
  • This post shows you how to handle user input. https://stackoverflow.com/questions/4730949/how-do-i-do-simple-user-input-in-python – ak_slick Aug 24 '18 at 13:09
  • apology for too broad description, next time ill be narrowing further. thanks for all the comments, great help for me to understand the problem. – newcodic2018 Aug 25 '18 at 01:17

2 Answers2

0

You can use the input() function in a list comprehension:

print([(int(input('Enter your number: ')), int(input('Enter your age: '))) for _ in range(int(input('Enter the number of questions: ')))])

Sample input and output:

Enter the number of questions: 2
Enter your number: 3
Enter your age: 23
Enter your number: 4
Enter your age: 35
[(3, 23), (4, 35)]
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

I have made a sample code:

x = input("How many times to run this program?\n>>> ")
output = []
for i in range(0,int(x)):
    output.append([input("Enter your number: "),input("Enter your age: ")])
print(output)

Executing:

How many times to run this program?
>>> 3
Enter your number: 1
Enter your age: 2
Enter your number: 3
Enter your age: 45
Enter your number: 56
Enter your age: 78
[['1', '2'], ['3', '45'], ['56', '78']]
Nouman
  • 6,947
  • 7
  • 32
  • 60