I have two separate txt files (reservations1, reservations2) that are set up like a reservation system for a restaurant.
Each line in the file stands for a reservation and has the format: ```Name, Time, Status```. Status is either ```CONFIRMED``` or ```CANCELLED``` .
I want to write a function ```show_reserva tions``` that takes as argument a string ```filename```, reads the file with the name ```filename``` and prints all ```CONFIRMED``` reservations in order of the time.
This has to be part of the code:
```
def show_reservatio ns(filename):
*(write your code here)
print("Reservat ions from first file:")
print(show_rese rvations("reser vations1.txt"))
print("Reservat ions from second file:")
print(show_rese rvations("reser vations2.txt"))
```
This is what I have done so far:
```
def show_reservatio ns(filename):
file = open(filename)
confirmed = {}
for line in file:
info_parts = line.split(", ")
if info_parts[2].strip() == "CONFIRMED" :
confirmed[int(info_parts[1].strip())] = info_parts[0].strip()
times = list(confirmed. keys())
times.sort()
sorted_confirme d = {}
for time in times:
sorted_confirme d[time] = confirmed[time]
return sorted_confirme d
print("Reservat ions from first file:")
print(show_rese rvations("reser vations1.txt"))
print("Reservat ions from second file:")
print(show_rese rvations("reser vations2.txt"))
```
The output is:
```
Reservations from first file:
{17: 'Asley', 18: 'Kira', 20: 'Jolyn'}
Reservations from second file:
{17: 'Codi', 18: 'Christel', 19: 'Valentine', 20: 'Eliseo'}
```
It is close to what I want. But I want it to look like this:
```
Reservations from first file:
Asley, 17
Kira, 18
Jolyn, 20
Reservations from second file:
Codi, 17
Christel, 18
Valentine, 19
Eliseo, 20
```
I want to solve it without having to write the dictionary in the code.
How do I correct it?
Each line in the file stands for a reservation and has the format: ```Name, Time, Status```. Status is either ```CONFIRMED``` or ```CANCELLED``` .
I want to write a function ```show_reserva tions``` that takes as argument a string ```filename```, reads the file with the name ```filename``` and prints all ```CONFIRMED``` reservations in order of the time.
This has to be part of the code:
```
def show_reservatio ns(filename):
*(write your code here)
print("Reservat ions from first file:")
print(show_rese rvations("reser vations1.txt"))
print("Reservat ions from second file:")
print(show_rese rvations("reser vations2.txt"))
```
This is what I have done so far:
```
def show_reservatio ns(filename):
file = open(filename)
confirmed = {}
for line in file:
info_parts = line.split(", ")
if info_parts[2].strip() == "CONFIRMED" :
confirmed[int(info_parts[1].strip())] = info_parts[0].strip()
times = list(confirmed. keys())
times.sort()
sorted_confirme d = {}
for time in times:
sorted_confirme d[time] = confirmed[time]
return sorted_confirme d
print("Reservat ions from first file:")
print(show_rese rvations("reser vations1.txt"))
print("Reservat ions from second file:")
print(show_rese rvations("reser vations2.txt"))
```
The output is:
```
Reservations from first file:
{17: 'Asley', 18: 'Kira', 20: 'Jolyn'}
Reservations from second file:
{17: 'Codi', 18: 'Christel', 19: 'Valentine', 20: 'Eliseo'}
```
It is close to what I want. But I want it to look like this:
```
Reservations from first file:
Asley, 17
Kira, 18
Jolyn, 20
Reservations from second file:
Codi, 17
Christel, 18
Valentine, 19
Eliseo, 20
```
I want to solve it without having to write the dictionary in the code.
How do I correct it?
Comment