[英]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:其实有两个问题:
final_list.append(subject_list)
as noted by Castiell正如 Castiell 所说,您忘记了final_list.append(subject_list)
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.