So I have written a program that takes in data for certain items, the code, the name and the price of the item. The program also allows users to see items and buy them, however it is all saved in a text file and I was wondering if it would be better in sqlite 3.
def enter_item_info():#the enter function
count = 0#various variables needed for validation and other.
validcodes =["11111115" , "11111122" , "11111139" , "11111146" , "11111153" , "11111160" , "11111177" , "11111184"]#the list of validcodes
data = [[0,1,2],[0,1,2],[0,1,2],[0,1,2],[0,1,2],[0,1,2],[0,1,2],[0,1,2]]#seting up the data variable to be able to take different groups of items
file = (open('Stock.txt','wb'))#opening the file, this also creates it if the file is not found
print("Please enter the appropraite data for products.")
while count < 8:#start of the loop
validcode = False#this variable resets every loop so each code is checked individually
data[count][0] = input("Code")#asking for the code
vdata = str(data[count][0])#turning it to a string for validation
while validcode == False:#start if the validation loop
if vdata in validcodes:#testing if the code is in the list previously established
print("Valid Code")
validcode = True#ends the loop if the code is valid
else:#if the code is invalid it will restart
print("Invalid code restarting")
menu()#back to menu
data[count][1] = input("Name")#asking for name
data[count][2] = input("Price")#asking for price
count = count + 1 #is increased each time so the loop ends after 8 runthroughs
print("")#makes the loops clear
pickle.dump(data , file)#saves the entered data in a text file that was previously established
file.close()
menu()#back to the menu
def read_item_info():# the read function
file=(open('Stock.txt','rb'))#opens the file that was made in the entering process
data=pickle.load(file)
loops = 0#the variable for the looping
while loops < 8:#start of the loop.
print("Code = " +data[loops][0] + " Name = " +data[loops][1] + " Price = £" +data[loops][2])#prints the first group
loops = loops + 1#this changes every loop so that different groups are printed each time
file.close()#closes the file
menu()#returns the user back to the menu
def shop_for_items():#starts the shop function
bought = 0#sets up the variables for the loop
items = 0
totalcost = 0#is the total cost
file=(open('Stock.txt','rb'))#opens the file
data=pickle.load(file)#sets the data variable to the open file
validcodes1 =["11111115" , "11111122" , "11111139" , "11111146" , "11111153" , "11111160" , "11111177" , "11111184"]
while bought < 4:#the shop loop
print("Would you like to buy some items? ")#Initial question
choice = input("Type y or n respectivily")#the choice to end loop or buy
if choice == "y":#the if statement
print("What is the code of the item you would like to buy?")
item = input("Code = ")#the user enters the code here
item = str(item)#this sets the variable to be a string
if item in validcodes1:
item1 = item[6]#this finds the codes location in the saved data
item1 = int(item1)#turns it back into an into so we can tally the total cost
print("That item is " + data[item1 - 1][1])#this gives sme basic information about the pruchased product
print("That costs " + data[item1 - 1][2])
cost = data[item1 - 1][2]#this sets a variable to be equal to the cost
stock = int(input("How many would you like to purchase? "))#ask the quantity
items = items + stock # this sets the totalitems
cost = int(cost) * stock #this is setting the total cost
totalcost = totalcost + int(cost)
else:
print("That is not a valid code")
elif choice == "n": #this ends the loop
print("You bought " , str(items) , " items for a total cost of £" , str(totalcost) , "Thank you for shopping")
bought = bought + 4 # this ends the loop
else:
print("That is incorrect input, please re enter")
menu()#sends the user back to the menu
Aucun commentaire:
Enregistrer un commentaire