i was modifying a program that i was writing to make it so that it wont continue on unless they put in a input that matches a word thats stored in a list. however after i did that, it would not let me print out the data i have, here is the code
while True:
dept = input('what department are you in right now: ')
dept = dept.upper()
if dept not in department_storage:
print("not approriate response")
continue
else:
break
if dept in department_storage:
department_url = requests.get(f"https://api.umd.io/v0/courses?dept_id={dept}")
specific_major =department_url.json()
keep_keys = ["course_id"]
courses = [{k: json_dict[k] for k in keep_keys}
for json_dict in specific_major]
#return courses,dept
print(courses)
im trying to print out the courses variable, however when i try and run the function it doesn't show the print output, and it doesn't show any errors when i run it so im lost on what i did wrong and how to fix it. i was wondering if i could ask the kind stack overflow community for help.
The break
command forces to exit the while loop and stop taking inputs.
Try:
if dept in department_storage:
...
# if dept not in department_storage
else:
...
You may not be getting the output for multiple reasons: -
dept not in department_storage
is breaking the loop if its true. break
gets you out of the loop. courses
is a null object, probably because the result you are getting has empty objects. You can add import pdb; pdb.set_trace();
import pdb; pdb.set_trace();
at various points in your code and write the variable names in the debugger to see the values. That will aid you in the investigation.
As per the requirement, my suggestion is the following code: -
while True:
dept = input('what department are you in right now: (type exit to quit)')
dept = dept.upper()
if dept == 'EXIT':
break
if dept not in department_storage:
print("this department does not exist, enter correct department")
else:
try:
department_url = requests.get(f"https://api.umd.io/v0/courses?dept_id={dept}")
specific_major =department_url.json()
keep_keys = ["course_id"]
courses = [{k: json_dict[k] for k in keep_keys}
for json_dict in specific_major]
print(courses)
except Exception as e:
print(e)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.