简体   繁体   中英

Is there a simple way to take user input as an integer and use that integer as an amount for how many times the next question is asked?

I am trying to create a list from user input but I need to find a way to ask how many integers the user wants, and take their answer to then ask them to enter an integer, depending on how many integers they want.

This is for my Computer Science homework and we are learning how to first use list and user input.

def numbers():
    list1 = []
    list_amount = int(input("How many integers would you like to add to the list? "))
    print("Enter an integer: ")
    for i in range(list_amount):
        print "Enter an integer: {}".format(list_amount)

I expect the output to look like this:

How many integers would you like to add to the list? 7
Enter an integer: 54
Enter an integer: 91
Enter an integer: 23
Enter an integer: 8
Enter an integer: -66
Enter an integer: 39
Enter an integer: 2

There is helpful comments on your question, if you still can't figure out how to do, here it is:

def numbers():
    list1 = []
    list_amount = int(input("How many integers would you like to add to the list? "))

    for i in range(list_amount):
        num = int(input("Enter an integer: "))
        list1.append(num)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM