Reputation: 29
The program prints out Peter: Number of completed courses: 2 ('Introduction to programming', 3), ('Math', 5)
But I want it to print out without the parentheses or commas, in my code I tried to do it like *students[name],sep=", ")
but it only removed the brackets only.
def add_student(students,name):
students[name] = set()
def print_student(students, name):
if name not in students:
print(f"{name} does not exist in the database")
else:
print(f"{name}:")
print(f"Number of completed courses: {len(students[name])}")
print(*students[name],sep=', ')
total_score = 0
for course in students[name]:
total_score += course[1]
try:
print(f"average grade : {total_score/len(students[name])}")
except ZeroDivisionError:
print("no completed courses")
def add_course(students,name, course:tuple):
if course[1] == 0:
return 0
students[name].add(course)
students = {}
add_student(students,"Ryan")
add_student(students,"Chris")
add_student(students,"Peter")
add_course(students,"Ryan",("Linear Algebra",9))
add_course(students,"Peter",("Math",5))
add_course(students,"Peter",("Program",0))
add_course(students,"Peter", ("Introduction to programming",3))
print_student(students,"Peter")
print_student(students,"Ryan")
Upvotes: 1
Views: 203
Reputation: 1117
A tuple is always ordered and unchangeable.
When you add courses down below in your code you are giving an argument as a tuple
add_course(students,"Peter",("Program",0))
# (tupItem0, tupItem1) <-- The basic form of a tuple
By simply printing a tuple you get the entire structure
"('Program',0)"
You get data out of a tuple like you do a list
foo = ('Program',0)
bar = foo[0] # Bar is now "Program"
In your program when you do this:
print(*students[name],sep=', ')
You are printing the entire list of tuples. For explanation purposed I'm going to do it like the following.
outString = ""
for item in students[name]:
outString += item[0] + ", "
print(outString[:-2])
Which should get you all of the courses out of your data structure
Math, Introduction to programming
Upvotes: 0
Reputation: 983
To print the content of a tuple with a simple separator, use join() as in:
print(" ".join(my_tuple))
will show the contents with spaces between. If you want them 'butt up' to each other...
print("".join(my_tuple))
With a 'nice' comma-space separator...
print(", ".join(my_tuple))
Upvotes: 0
Reputation: 1052
This should be what you are looking for
for course in students[name]:
print(f"{course[0]} ({course[1]})")
output:
Introduction to programming (3)
Math (5)
Upvotes: 2