[英]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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.