Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

After countless attempts to solve my issue and using chegg, I can not figure out why my code is not connecting my trunk lines correctly.

After countless attempts to solve my issue and using chegg, I can not figure out why my code is not connecting my trunk lines correctly. I will send the inputs, but my code is suppose to use recursion somewhere within my code but even with it, i am still not getting the desired result. whenever i switch-connect 2 phone numbers and start-call between them, it's suppose to say that the numbers are connected, but it's not. Could someone please help me fix my issue?
code:
import json
HYPHEN ="-"
QUIT = 'quit'
SWITCH_CONNECT = 'switch-connect'
SWITCH_ADD = 'switch-add'
PHONE_ADD = 'phone-add'
NETWORK_SAVE = 'network-save'
NETWORK_LOAD = 'network-load'
START_CALL = 'start-call'
END_CALL = 'end-call'
DISPLAY = 'display'
def connect_switchboards(switchboards, area_1, area_2):
if area_1 not in switchboards:
switchboards[area_1]={}
if area_2 not in switchboards:
switchboards[area_2]={}
switchboards[area_1][area_2]=[]
switchboards[area_2][area_1]=[]
def add_switchboard(switchboards, area_code):
# Add a new switchboard to the network
switchboards[area_code]={}
def add_phone(switchboards, area_code, phone_number):
# Add a new phone to the network
if area_code not in switchboards:
switchboards[area_code]={}
switchboards[area_code][phone_number]=[]
def save_network(switchboards, file_name):
# Save the network to a file
with open(file_name, 'w') as f:
json.dump(switchboards, f)
def load_network(file_name):
# Load the network from a file
with open(file_name, 'r') as f:
switchboards = json.load(f)
return switchboards
def start_call(switchboards, start_area, start_number, end_area, end_number):
# Start a call between two phones
if start_area not in switchboards or end_area not in switchboards:
print('Invalid area code.')
return
start_phone = str(start_number)
end_phone = str(end_number)
if start_area == end_area:
# Local call
switchboards[start_area].setdefault(start_phone, []).append(end_phone)
print(f'{start_area}-{start_phone} and {end_area}-{end_phone} are now connected.')
elif switchboards[start_area].get(end_area) or switchboards[end_area].get(start_area):
# Long-distance call
if end_area not in switchboards[start_area]:
print(f'Connecting switchboards {start_area} and {end_area}.')
connect_switchboards(switchboards, start_area, end_area)
switchboards[start_area][start_phone].append(end_phone)
print(f'{start_area}-{start_phone} and {end_area}-{end_phone} are now connected.')
else:
print(f'Cannot connect phone {start_area}-{start_phone} to phone {end_area}-{end_phone}.')
def end_call(switchboards, start_area, start_number):
# End a call between two phones
if len(switchboards[start_area][start_number])>0:
switchboards[start_area][start_number].pop()
else:
print(f'No call in progress for phone {start_number}.')
def display_switchboard(switchboards, switchboard):
print('Switchboard with area code: '+ str(switchboard))
print(' Trunk lines are:')
for connected_area in switchboards[switchboard]:
print(' Trunk line connection to: '+ str(connected_area))
print(' Local phone numbers are:')
for phone_number, connections in switchboards[switchboard].items():
if not connections:
print(' Phone with number: '+ str(phone_number)+' is not in use.')
else:
for connection in connections:
print(' Phone with number: '+ str(connection)+' is in use.')
def display(switchboards):
# Display the network
for switchboard in switchboards:
display_switchboard(switchboards, switchboard)
inputs:
switch-add 443
switch-add 410
switch-add 656
display
switch-connect 443410
phone-add 656-112-3412
phone-add 443-132-1332
display
start-call 656-112-3412443-132-1332
switch-connect 656410
start-call 656-112-3412443-132-1332
(main function will be sent as picture)
The issue is after using start-call the first time, it's suppose to say that the phone lines are not connected, but using switch-connect is suppose to connect these numbers together to allow them to call, but within my code the issue is that it's not saying that it's connected.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

Test the series for convergence or divergence. E (/7 1)"

Answered: 1 week ago