繁体   English   中英

是否有一种简单的方法可以将用户输入作为整数使用,并将该整数用作下一个问题被问多少次的量?

[英]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?

我正在尝试从用户输入创建一个列表,但我需要找到一种方法来询问用户想要多少个整数,然后根据他们想要的整数数量,让他们回答然后让他们输入一个整数。

这是我的计算机科学作业,我们正在学习如何首先使用列表和用户输入。

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)

我希望输出看起来像这样:

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

对你的问题有一些有用的评论,如果你仍然无法弄清楚如何做,这里是:

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.

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