def add():
import add_coffee_record
import imp
imp.reload(add_coffee_record)
def show():
import show_coffee_records
import imp
imp.reload(show_coffee_records)
def search():
import search_coffee_records
import imp
imp.reload(search_coffee_records)
def modify():
import modify_coffee_records
import imp
imp.reload(modify_coffee_records)
def delete():
import delete_coffee_record
import imp
imp.reload(delete_coffee_record)
def main():
num=input('\nEnter the number on the menu: ')
while num != '6':
if num == '1':
print()
add()
if num == '2':
print()
show()
if num == '3':
print()
search()
if num == '4':
print()
modify()
if num == '5':
print()
delete()
num=input('\nEnter the number on the menu: ')
main()
My output looks like so:
Enter the number on the menu: 2
Description: Thanksgiving Blend
Quantity: 300.0
Description: Christmas Blend
Quantity: 100.0
Description: Thanksgiving Blend
Quantity: 300.0
Description: Christmas Blend
Quantity: 100.0
Enter the number on the menu: 2
Description: Thanksgiving Blend
Quantity: 300.0
Description: Christmas Blend
Quantity: 100.0
Enter the number on the menu: 2
Description: Thanksgiving Blend
Quantity: 300.0
Description: Christmas Blend
Quantity: 100.0
This can't happen. The double output at the beginning is just plain wrong. I need it to output the file ONCE each time. Is there a way to do this? For some reason it outputs a double on the first request and then accesses it normally after the first request. And I think I might understand why: the first time the function is called inside the while loop, it shows 2 things: the import and the reload. The second and successive time it's called from the while loop it ignores the import and goes straight for the reload. That's why it's happening. So great. I've diagnosed the problem. But how do I solve it? I've thought of some solutions:
I thought about creating an if statement inside (for example) def add(): count+=1 Wherein a counter would say something like "if count=1 run import add_coffee_record" and then after that if count>1 then run the import imp and reloader. You know what I'm saying? Does that make sense? I thought it might work so that it accesses the import on the FIRST input and then on each successive count after the first count it acessess the RELOAD function. Can I do this? Any help would be appreciated. I tried finagling it, but the count thing didn't work out. I'm not sure if I'd have to put a counter in every function above the main or what. It's just so confusing. Here is my proposed plan to make this work:
count=0
def show():
count+=1
if count<2:
import show_coffee_records
else:
import imp
imp.reload(show_coffee_records)
NOTE: I CANNOT TRANSFER THE FILE CONTENTS INTO THIS PROGRAM. IT MUST ACT AS A STAND-ALONE MENU THAT ACCESSES THOSE FILES VIA IMPORT. PLEASE DO NOT ASK ME TO DO OTHERWISE -- THIS WAS ASSIGNED TO ME BY TEACHER THANK YOU.
Aucun commentaire:
Enregistrer un commentaire