Unverstänlicher TypeError
Verfasst: Samstag 9. November 2019, 15:30
Hallo,
ich habe ein Programm geschrieben, dass zufällig (Modul random) eine Person aus einer Liste mit Personen auswählen kann oder zufällige Gruppen bildet.
Beim Ausführen des Programms habe ich aber leider festgestellt, dass es bei der 2 Wiederholung zu einem TypeError kommt,
welchen ich allerdings nicht verstehen oder nachvollziehen kann. Kann mir vllt. jemand erläutern, was der Fehler ist.
Ich habe die Idee, das es damit zusammen hängt, dass aus der list_of_people beim ersten Durchlauf Sachen entfernt werden, wodurch es beim zweiten mal nicht läuft.
Allerdings weiß ich nicht wieso das sein könnte, da ich die Liste ja nur als Argument übergebe und nicht Veränderungen vornehmen lasse.
Außerdem bin ich mir unsicher, welche Version ich in Zeile 47-57 brauche.
Console:
Enter groups and comma separated the size of them with or person
if you want the Program to make random groups or chose a random person!
(groups,(number)/person)groups,4
Here are the groups
['Megan', 'Darleen', 'Inez', 'Fleta']
['Tobi', 'Adrien', 'Sixta', 'Peg']
['Sun', 'Martha', 'Johanna', 'Jen']
['Sibyl', 'Camilla', 'Tyson', 'Dario']
['Anna', 'Stefanie', 'Arden', 'Roselia']
Enter groups and comma separated the size of them with or person
if you want the Program to make random groups or chose a random person!
(groups,(number)/person)groups,4
Traceback (most recent call last):
There are not enough people to make groups. Here are all people: []
File "C:/Users/Private/PycharmProjects/Python_for_Teachers/Chose_People_from_List.py", line 54, in <module>
Here are the groups
for i in range(len(list_of_groups)): # Should print the groups, but here occurs the error if input is an even number
TypeError: object of type 'NoneType' has no len()
Process finished with exit code 1
Danke im Voraus
PS: Wie nutze ich bei PyCharm die Debug Funktion?
ich habe ein Programm geschrieben, dass zufällig (Modul random) eine Person aus einer Liste mit Personen auswählen kann oder zufällige Gruppen bildet.
Beim Ausführen des Programms habe ich aber leider festgestellt, dass es bei der 2 Wiederholung zu einem TypeError kommt,
welchen ich allerdings nicht verstehen oder nachvollziehen kann. Kann mir vllt. jemand erläutern, was der Fehler ist.
Ich habe die Idee, das es damit zusammen hängt, dass aus der list_of_people beim ersten Durchlauf Sachen entfernt werden, wodurch es beim zweiten mal nicht läuft.
Allerdings weiß ich nicht wieso das sein könnte, da ich die Liste ja nur als Argument übergebe und nicht Veränderungen vornehmen lasse.
Außerdem bin ich mir unsicher, welche Version ich in Zeile 47-57 brauche.
Code: Alles auswählen
# This Program should make it possible to chose a random Person from a list or to make random groups
# Imports
from random import*
# Fill this List with the names of the people
list_of_people = ["Camilla", "Sixta", "Inez", "Jen", "Stefanie", "Darleen", "Martha", "Peg", "Johanna", "Sun",
"Tobi", "Fleta", "Arden", "Adrien", "Megan", "Sibyl", "Dario", "Roselia", "Tyson", "Anna"]
def make_groups(size_of_groups, list_of_people_arg):
"""Takes the group size and the list of people and returns a list with the groups."""
# list_of_people_arg = list_of_people_arg
local_list = [] # List to append at list_of_groups
list_of_groups = [] # Will store the different groups
number_of_people = len(list_of_people_arg)
if not number_of_people < size_of_groups: # Checking for a valid group size
number_of_remaining_people = number_of_people # Assign loop variable
while number_of_remaining_people >= size_of_groups: # Checking if enough people are in the list
number_of_remaining_people -= size_of_groups
for i in range(size_of_groups): # Loop for adding people to the lists
local_list.append(choice(list_of_people_arg))
list_of_people_arg.remove(local_list[-1])
list_of_groups.append(local_list)
local_list = []
return list_of_groups
else:
print(f"There are not enough people to make groups. Here are all people: {list_of_people_arg}")
while True:
input1 = input("""Enter groups and comma separated the size of them with or person
if you want the Program to make random groups or chose a random person!
(groups,(number)/person)""").lower()
split_input1 = input1.split(",")
if 2 == len(split_input1): # Checks if groups was chosen
try:
split_input1[1] = int(split_input1[1])
except ValueError:
print("Need valid input!")
continue
if split_input1[0] == "groups":
list_of_groups = make_groups(split_input1[1], list_of_people) # --) Function
"""if not len(list_of_people) > int(split_input1[1]): #
print("Here are the groups")
for i in range(len(list_of_groups)): # Should print the groups, but here occurs the error if input is an even number
print(list_of_groups[i])
if len(list_of_people) != 0:
print(list_of_people)"""
print("Here are the groups")
for i in range(len(list_of_groups)): # Should print the groups, but here occurs the error if input is an even number
print(list_of_groups[i])
if len(list_of_people) != 0:
print(list_of_people)
elif split_input1[0] == "person": # Simple code for choosing a person
random_person = choice(list_of_people)
print(random_person)
else:
print("Need valid input!")
Enter groups and comma separated the size of them with or person
if you want the Program to make random groups or chose a random person!
(groups,(number)/person)groups,4
Here are the groups
['Megan', 'Darleen', 'Inez', 'Fleta']
['Tobi', 'Adrien', 'Sixta', 'Peg']
['Sun', 'Martha', 'Johanna', 'Jen']
['Sibyl', 'Camilla', 'Tyson', 'Dario']
['Anna', 'Stefanie', 'Arden', 'Roselia']
Enter groups and comma separated the size of them with or person
if you want the Program to make random groups or chose a random person!
(groups,(number)/person)groups,4
Traceback (most recent call last):
There are not enough people to make groups. Here are all people: []
File "C:/Users/Private/PycharmProjects/Python_for_Teachers/Chose_People_from_List.py", line 54, in <module>
Here are the groups
for i in range(len(list_of_groups)): # Should print the groups, but here occurs the error if input is an even number
TypeError: object of type 'NoneType' has no len()
Process finished with exit code 1
Danke im Voraus
PS: Wie nutze ich bei PyCharm die Debug Funktion?