1

I AM NEW TO PYTHON AND CODING IN GENERAL. So I have a program with a menu that has multiple functions in it. individually each function works fine on its own, however when i put them together they would usually not fully execute and instead stop half way or wont work at all. EXAMPLE- the function remove wont remove what i tell it to. def show_coffee will only show the first description and weight only and nothing else. What can i do to make the functions fully execute?

import os
def main():
choice =''
fun=[]
while choice != 4:
    menu()
    choice=getUserChoice()
    if choice !=4:
        fun=process_choice(choice,fun)
        print(fun)

print("Goodby!")

def process_choice(choice,fun):
#fun=fun1
if choice == 0:
    fun=add_coffee(fun)
elif choice == 1:
    fun=show_coffee(fun)
elif choice == 2:
    fun=search_coffee(fun)
elif choice == 3:
    fun=modify_coffee(fun)
else:
    print(choice,"is not a valid choice.")
return fun


def add_coffee(fun):
another= 'y'
coffee_file=open('coffee.txt', 'a')
Description={}
while another == 'y' or another == 'Y':
    print('Enter the following coffee data:')
    descr=input('Description: ')
    qty= int(input('Quantity (in pounds): '))
    coffee_file.write(descr + '\n')
    coffee_file.write(str(qty) + '\n')
    print("Do you want to add another record?")
    another = input("Y=yes, anything else =no: ")

    return fun


coffee_file.close()
print('Data append to coffee.txt.')


def show_coffee(fun2):
coffee_file=open ('coffee.txt', 'r')
descr=coffee_file.readline()
while descr != "":

    qty= str(coffee_file.readline())

    descr=descr.rstrip('\n')
    print('Description:', descr)
    print('Quantity:', qty)
    descr= coffee_file.readline()
    fun=fun2
    return fun
coffee_file.close()

def search_coffee(fun3):
found=False
search =input('Enter a description to search for: ')
coffee_file=open('coffee.txt', 'r')
descr=coffee_file.readline()
while descr != '':
    qty= float(coffee_file.readline())
    descr = descr.rstrip('\n')
    if descr== search:
        print('Description:', descr)
        print('Quantity:', qty)
        found=True
    descr=coffee_file.readline()
    fun=fun3
    return fun
coffee_file.close()
if not found:
    print('That item was not found in the file.')




def modify_coffee(fun4):
found=False
search=input('Which coffee do you want to delete? ')
coffee_file=open('coffee.txt', 'r')
temp_file=open('temp.txt', 'w')
descr=coffee_file.readline()
while descr != '':
    qty=float(coffee_file.readline())
    descr=descr.rstrip('\n')
    if descr !=search:
        temp_file.write(descr + '\n')
        temp_file.write(str(qty) + '\n')
    else:
        found=True
    descr=coffee_file.readline()
    fun=fun4
    return fun
coffee_file.close()
temp_file.close()
os.remove('coffee.txt')
os.rename('temp.txt', 'coffee.txt')
if found:
    print('The file has been update.')
else:
    print('The item was not found in the file.')




def menu():
print('''
0. Add or Update an entry
1. Show an entry
2. Search
3. remove
4. Remove number
''')


def getUserChoice():
choice=-1
while choice <0 or choice > 3:
    print("Please select 0-3: ",end='')
    choice=int(input())
return choice
cobra
  • 13
  • 2
  • Please get your indentation correct – biobirdman Nov 25 '14 at 03:52
  • In general you can use `return` in a function once. There can be multiple `return` statements but only one is executed. At the return the function will stop executing. If you use a while loop a return will break out of the loop. – Klaus D. Nov 25 '14 at 04:30

1 Answers1

0

You are defining functions but this does not call a function. The standard way to do this in Python is use the if __name__=="__main__": statement at the bottom of a file. When the file is executed (instead of functions/classes being imported by another script) the code block within the scope of if __name__=="__main__": is executed.

Get comfortable with this, it's useful and clean :) Good read - What does if __name__ == "__main__": do?

So, for example...

At the bottom of your file...

if __name__=="__main__":
    main()
Community
  • 1
  • 1
Ian Price
  • 7,416
  • 2
  • 23
  • 34