Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 编写代码以插入15个数字(0-7之间)的练习_Python 3.x - Fatal编程技术网

Python 3.x 编写代码以插入15个数字(0-7之间)的练习

Python 3.x 编写代码以插入15个数字(0-7之间)的练习,python-3.x,Python 3.x,帮助完成以下练习 . 写一个代码在列表中插入15个介于0-7之间的数字 将列表转换为元组 打印0-7之间的每个数字的计数 在元组中查找最大值和最小值 打印已排序的元组 在列表中插入0-7之间的15个数字 numbers1 = [] numbers2 = [] for i in range(1,8): numbers1.append(i) for x in range(15,30): numbers2.append(x) numbers1.insert(-1,num

帮助完成以下练习
. 写一个代码在列表中插入15个介于0-7之间的数字 将列表转换为元组 打印0-7之间的每个数字的计数 在元组中查找最大值和最小值 打印已排序的元组

在列表中插入0-7之间的15个数字

numbers1 = []
numbers2 = []
for i in range(1,8):
    numbers1.append(i)
    for x in range(15,30):
    numbers2.append(x)
    numbers1.insert(-1,numbers2)
    print(numbers1)
## convert the list to tuple
tuple1 = tuple(numbers1)
print(tuple1)

使用“.sort”将列表按字母顺序、数字顺序等进行排列。您将查找以下内容:

#Define a list
number_list = []

#loop 15 times
for number in range(5):

    #this while loop loops within the for loop until a valid input is given
    while True:

        #try catch statements when attempting to convert to integer
        try:
            #define number_input as an integer input
            number_input = int(input("Please enter a number:\n"))
        #if failed to convert to an integer
        except:
            #print warning
            print("Please enter a valid number\n")
            #'continue' starts the program back at the begining of the while loop
            continue

        #if the number is equal to or below 7, and equal or above 0
        if(number_input <= 7 and number_input >= 0):
            #end the while loop
            break
        #if not between the values
        else:
            #print warning
            print("Please enter a valid number\n")

    #append integer to the list and go continue the for loop
    number_list.append(number_input)

#sort the list in order of smallest to largest
number_list.sort()
#convert number_list to tuple
number_list = tuple(number_list)

print(number_list)

欢迎来到堆栈溢出!请拿着这本书,读一读。请在你的问题中明确提出一个具体问题。描述你希望你的代码做什么,你期望得到什么输出,你实际得到什么输出。此外,由于这看起来像是家庭作业,请先自己诚实地尝试一下。您包含的示例不计算计数或最小值和最大值。编写代码在列表中插入15个介于0-7之间的数字将列表转换为元组打印0-7之间的每个数字的计数在元组中找到最大值和最小值打印排序后的元组我运行代码,输出为4、5、6、7,7使用min max查找元组中的最小值和最大值谢谢,对于循环15次,5是否应更改为15?对于范围5中的数字:还有什么正确的方法可以对0-7之间的每个数字进行计数