简体   繁体   English

我不知道我的代码有什么问题(刚开始 python)

[英]I dont know what is wrong with my code (just started python)

i have made a list by using while list and then used if statements.我使用 while 列表创建了一个列表,然后使用了 if 语句。 In the first two conditions, the code runs perfectly without any error.在前两个条件下,代码运行完美,没有任何错误。 But in the third condition it is showing that the list is empty.但在第三种情况下,它显示列表为空。 I have no clue what is wrong here.我不知道这里有什么问题。

CODE:代码:

# time table generator with python

import random
no_of_classes = int(input("Please enter the number of classes in a day: "))
name_of_classes = input("Please enter the subject name/name of classes (please separate then by commas): ")
class_name_list = name_of_classes.split(",")
days_in_week = int(input("Enter the number of days in a week for which the school is working:"))
list_1 = [] `list with the problem`
x = 1
while x <= no_of_classes:
    list_1.append(x)
    x += 1

final_list = []
for j in range(days_in_week):
    subject_list = []
    if no_of_classes > len(class_name_list):
        for i in class_name_list:
            a = random.choice(list_1)
            subject_list.insert((a - 1), i)
        for m in range(no_of_classes - len(class_name_list)):
            b = random.choice(class_name_list)
            subject_list.append(b)
        final_list.append(subject_list)

    elif no_of_classes == len(class_name_list):
        for i in class_name_list:
            a = random.choice(list_1)
            subject_list.insert((a-1), i)
        final_list.append(subject_list)

    else:   `having problem with this condition`
        temp_class_list = []
        list_2 = class_name_list
        for m in range(no_of_classes):
            n = random.choice(list_2)
            a = random.choice(list_1)
            list_1.remove(a)
            list_2.remove(n)
            subject_list.insert((a-1), n)

for k in range(days_in_week):
    print(final_list[k])

OUTPUT: OUTPUT:

Traceback (most recent call last):
  File "/Users/arungupta/Documents/trial (delete).py", line 24, in <module>
    a = random.choice(list_1)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/random.py", line 301, in choice
    raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence

THANK YOU FOR YOUR HELP.感谢您的帮助。 MUCH APPRECIATED!!!非常感激!!!

You just forgot to fill the final_list: final_list.append(subject_list)你只是忘了填写 final_list:final_list.append(subject_list)

else:   
    temp_class_list = []
    list_2 = class_name_list
    for m in range(no_of_classes):
        n = random.choice(list_2)
        a = random.choice(list_1)
        list_1.remove(a)
        list_2.remove(n)
        subject_list.insert((a-1), n)
        final_list.append(subject_list)

There are two problems actually:其实有两个问题:

  • You forgot final_list.append(subject_list) as noted by Castiell正如 Castiell 所说,您忘记了final_list.append(subject_list)
  • You emptied list_1 , which makes your program crash in the next iteration (and actually, the error message you showed is due to this)您清空了list_1 ,这使您的程序在下一次迭代中崩溃(实际上,您显示的错误消息是由于这个原因)

Here is my proposed correction: make a copy of list_1 before you empty it:这是我建议的更正:在清空list_1之前复制它:

    else:
        temp_class_list = []
        list_2_copy = class_name_list.copy()
        list_1_copy = list_1.copy()
        for m in range(no_of_classes):
            n = random.choice(list_2_copy)
            a = random.choice(list_1_copy)
            list_1_copy.remove(a)
            list_2_copy.remove(n)
            subject_list.insert((a-1), n)
        final_list.append(subject_list)

(I also made a copy of class_name_list because I suspect it is the source of another undiscovered bug) (我还复制了class_name_list因为我怀疑它是另一个未发现错误的来源)

暂无
暂无

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

相关问题 我不知道我的代码有什么问题。 当我运行代码时它只是在一个循环中并且不会停止循环 - i dont know what is wrong with my code. When i run the code it is just in a loop and wont stop the loop 我不知道我的 tkinter 项目有什么问题 - I dont know what is wrong with my tkinter project 在我的代码中出现 EOF 错误,我不知道这意味着什么 - Getting an EOF Error in my code and i dont know what that means 对于 euler 4 号项目,我只是想知道我的代码有什么问题。 一直在敲我的大脑,我不知道怎么做,这是在 python - for project euler number 4 i am just wondering what is wrong with my code. have been knacking my brain and i do not know how, this is on python 由于某种原因,在我的代码中出现“索引超出范围”的错误,我实际上刚刚开始使用 Python,因此不知道如何解决这个问题 - In My Code There Is A Error Saying "Index Out Of Range" For Some Reason, I Actually Just Started Python And Hence Don't Know How To Solve This 我刚刚开始使用 Python,但我的代码无法正常工作 - I've just started Python and my code isn't working 嗨,我正在自己学习 python,想知道我的代码用于反转链表 python 有什么问题 - Hi, I was learning python on my own and wanted to know what was wrong with my code for reversing a linked list python 我的代码输入了无效的语法,但我不知道我的变量出了什么问题 - my code puts out invalid syntax but i dont know whats wrong with my variable Python的新手,不知道我的代码出了什么问题 - New to Python, don't know what is wrong with my code 我不知道这个python代码有什么问题 - I don't know what is wrong with this python code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM