Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, what's the method to ask for a user input based on previous user input?

Tags:

python

list

input

I have an assignment where I need to ask the user to input the total number of integers that will be in the data set, ask for each of the integers, and then add them into a list, then summarize the list into a dictionary.

For now I'm only worried about the user input part.

Here's what I have so far:

data = []
summary = {}

total_ints = int(raw_input('Total integers in data: '))

while total_ints:
  each_int = int(raw_input('Enter an integer: '))

data.append(each_int)

Basically I need to only print the 'each_int' input based on the number the user enters in 'total_ints'. I know the while loop isn't correct, so any help would be greatly appreciated.

Thanks!

like image 909
user1186420 Avatar asked Mar 22 '26 23:03

user1186420


1 Answers

This is a loop whose body is executed total_ints times:

for i in range(total_ints):
  data.append(int(raw_input('Enter an integer: ')))
like image 72
Scott Hunter Avatar answered Mar 24 '26 11:03

Scott Hunter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!