d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > General Archive > Any Gcers Do Any Python Programming? > Lmk If U Can Help W/ This
Prev12
Add Reply New Topic
Member
Posts: 161,550
Joined: Oct 18 2006
Gold: 4.03
Warn: 20%
Jul 10 2015 02:32pm
Learn try & except loop

Code

try:

except KeyboardInterrupt:


http://stackoverflow.com/questions/8645632/python-exit-infinite-while-loop-with-keyboardinterrupt-exception

http://effbot.org/zone/stupid-exceptions-keyboardinterrupt.htm


or you can just put if statements on your main loop that look for enter and exit when it's pressed
Member
Posts: 20,253
Joined: Apr 30 2008
Gold: 5,267.97
Jul 10 2015 04:19pm
Holy spaghetti code. Looping main, seriously?

I'm not even going to begin correcting this on my phone.
Member
Posts: 16,455
Joined: May 2 2007
Gold: 20.17
Jul 10 2015 07:49pm
Quote (howtodisappearcompletely @ Jul 10 2015 05:19pm)
Holy spaghetti code. Looping main, seriously?

I'm not even going to begin correcting this on my phone.


lol u think that's bad, this was the next assignment.

Code
array1zip=[0]*6 #Array Slot Declaration
array1zip[0]=48093 #Array Value Declaration
array1zip[1]=180 #Array Value Declaration
array1zip[2]=100 #Array Value Declaration
array1zip[3]=200 #Array Value Declaration
array1zip[4]=180 #Array Value Declaration
array1zip[5]=150 #Array Value Declaration
array2zip=[0]*6 #Array Slot Declaration
array2zip[0]=48088 #Array Value Declaration
array2zip[1]=270 #Array Value Declaration
array2zip[2]=330 #Array Value Declaration
array2zip[3]=160 #Array Value Declaration
array2zip[4]=150 #Array Value Declaration
array2zip[5]=250 #Array Value Declaration
array3zip=[0]*6 #Array Slot Declaration
array3zip[0]=48026 #Array Value Declaration
array3zip[1]=240 #Array Value Declaration
array3zip[2]=310 #Array Value Declaration
array3zip[3]=450 #Array Value Declaration
array3zip[4]=100 #Array Value Declaration
array3zip[5]=320 #Array Value Declaration
array4zip=[0]*6 #Array Slot Declaration
array4zip[0]=48066 #Array Value Declaration
array4zip[1]=200 #Array Value Declaration
array4zip[2]=230 #Array Value Declaration
array4zip[3]=350 #Array Value Declaration
array4zip[4]=110 #Array Value Declaration
array4zip[5]=360 #Array Value Declaration
menuselection = "" #menuselection declaration as string
def welcome(): #welcome function
print ("Welcome to Wy'East Coffee program") #Welcome print statement
print ("*********************************************") #printed line

def menu(menuselection): #Menuselection function
totalsales = 0.0 #Variable Declaration
zipcode = 0 #Variable Declaration
type1 = 0.0 #Variable Declaration
gtotal = 0.0 #Variable Declaration
hzip = 0.0 #Variable Declaration
zipcode1 = 0 #Variable Declaration
value = 0.0 #Variable Declaration
menuselection = "" #Variable Declaration
menuselection = str(input("What do you want to do?\n\n\t\tZ\t=\tGet Total Sales by Zip Code\n\t\tC\t=\tGet Total Sales by Coffee Type\n\t\tG\t=\tGet Grand Total Sales of all Coffee Sales\n\t\tHZ\t=\tGet Highest Sales by Zip Code\n\t\tHC\t=\tGet Highest Sales by Coffee Type\n\t\tE\t=\tExit\n*********************************************\nEnter your Choice ==>")) #Menuselection Input
if menuselection == "Z": # if menuselection is Z
totalsales = zipcodesales(totalsales) #if the selection matches, it runs the zipcodesales function
elif menuselection == "C": #if menuselection is C
type1 = coffeetype(type1) #if the selection matches, it runs the coffeetype function
elif menuselection == "G": #if menuselection is G
gtotal = grandtotal(gtotal) #if the selection matches, it runs the grandtotal function
elif menuselection == "HZ": #if menuselection is HZ
zipcode1 = highzip(hzip) #if the selection matches, it runs the highzip function
elif menuselection == "HC": #if menuselection is HC
value = hightype(value) #if the selection matches, it runs the hightype function
elif menuselection == "E": #if menuselection is E
return menuselection #if the selection matches, it returns the menuselection to the loop to exit the program
else: #else statement
print("Sorry you did not enter C,G,HZ,HC or E. Please Enter one of those letters and try again.") #try again print statement
menu(menuselection) #Runs the menu function again if the wrong data was entered



def inputdata(zipcode): #inputdata function for zipcode that is used in a couple of places throughout the program
zipcode = 0 #Zipcode declaration
zipcode = int(input("Please Enter The Zipcode That You Wish to Get The Sales For")) #zipcode input
return zipcode #returns the zipcode to other functions throughout the program


def inputcoffeetype(coftype): #inputcoffeetype function that is used in a couple of places throughout the program
coftype = "" #coftype declaration
coftype = str(input("Coffee Types:\n\t\tM = mocha\n\t\tL = latte\n\t\tR = regular\n\t\tD = decaf\n\t\tC = caramel\n\nEnter your choice: ")) #coftype input
return coftype #returns the coftype variable to other functions throughout the program

def coffeetype(type1): #coffeetype function
coftype = "" #coftype declaration
coftype = inputcoffeetype(coftype) #runs the inputcoffeetype function
if coftype == "M": #if coftype = M
type1 = (array1zip[1]*3.50)+(array2zip[1]*3.50)+(array3zip[1]*3.50)+(array4zip[1]*3.50) #Formula for mocha totals
print("Coffee type Mocha had total sales of: $",format(type1,',.2f')) #print statement for mocha totals
elif coftype == "L": #if coftype = L
type1 = (array1zip[2]*4.00)+(array2zip[2]*4.00)+(array3zip[2]*4.00)+(array4zip[2]*4.00) #Formula for Latte totals
print("Coffee type Latte had total sales of: $",format(type1,',.2f')) #print statement for Latte totals
elif coftype == "R": #if coftype = R
type1 = (array1zip[3]*2.50)+(array2zip[3]*2.50)+(array3zip[3]*2.50)+(array4zip[3]*2.50)#Formula for Regular totals
print("Coffee type Regular had total sales of: $",format(type1,',.2f')) #Print statement for regular totals
elif coftype == "D": #if coftype = D
type1 = (array1zip[4]*2.50)+(array2zip[4]*2.50)+(array3zip[4]*2.50)+(array4zip[4]*2.50) #formula for decaf totals
print("Coffee type Decaf had total sales of: $",format(type1,',.2f')) #print statement for decaf totals
elif coftype == "C": #if coftype = C
type1 = (array1zip[5]*4.50)+(array2zip[5]*4.50)+(array3zip[5]*4.50)+(array4zip[5]*4.50) #Formula for caramel totals
print("Coffee type Caramel had total sales of: $",format(type1,',.2f')) #Print statement for caramel totals
else: #else statement
print ("Sorry you did not enter M, L, R, D or C - Please Enter One of those letters") #Print statement for wrong info
coffeetype(type1) #Re-Run coffeetype if wrong info was entered
return type1 #return type1 variable to other parts of the program

def grandtotal(gtotal): #grandtotal function
gtotal = 0.0 #variable declaration
gtotal = (array1zip[1]*3.50)+(array1zip[2]*4.00)+(array1zip[3]*2.50)+(array1zip[4]*2.50)+(array1zip[5]*4.50)+(array2zip[1]*3.50)+(array2zip[2]*4.00)+(array2zip[3]*2.50)+(array2zip[4]*2.50)+(array2zip[5]*4.50)+(array3zip[1]*3.50)+(array3zip[2]*4.00)+(array3zip[3]*2.50)+(array3zip[4]*2.50)+(array3zip[5]*4.50)+(array4zip[1]*3.50)+(array4zip[2]*4.00)+(array4zip[3]*2.50)+(array4zip[4]*2.50)+(array4zip[5]*4.50) #Grand total formula
print ("The Grand Total for All Sales Is : $",format(gtotal,',.2f')) #Grand total print statement
return gtotal #returns gtotal to other parts of the program

def highzip(hzip): #highzip function
hzip = 0.0 #variable declaration
zipcode = 0 #variable declaration
zipcode = inputdata(zipcode) #runs the inputdata zipcode function
if zipcode == 48093: #if zipcode = 48093
hzip = array1zip[3]*2.50 #formula for 48093 highzip
print("For Zipcode 48093 Regular sold the most units and made : $",format(hzip,',.2f')) #print statement for 48093 highzip
elif zipcode == 48088: #if zipcode = 48088
hzip = array2zip[2]*4.00 #formula for 48088 highzip
print("For Zipcode 48088 Latte sold the most units and made : $",format(hzip,',.2f'))
elif zipcode == 48026: #if zipcode = 48026
hzip = array3zip[3]*2.50 #formula for 48026 highzip
print("For Zipcode 48026 Regular sold the most units and made : $",format(hzip,',.2f')) #print statement for 48026 highzip
elif zipcode == 48066: #if zipcode = 48066
hzip = array4zip[5]*4.50 #formula for 48066 highzip
print("For Zipcode 48066 Caramel sold the most units and made : $",format(hzip,',.2f')) #print statement for 48066 highzip
else: #else statement
print("You entered the wrong zipcode, please enter 48093,48088,48026, or 48066") #print statement for wrong info
highzip(hzip) #re-run highzip function if wrong info was entered
return hzip #Return hzip variable to other parts of the program

def hightype(value): #hightype function
coftype = "" #Variable declaration
value = 0.0 #Variable declaration
coftype = inputcoffeetype(coftype) #runs the inputcoffeetype function
if coftype == "M": #If coftype = M
value = (array2zip[1]*3.50) #Formula for high sales of mocha
print("The zipcode with the highest sales is: 48088") #print statement for zipcode
print("Total Sales : $",format(value,',.2f')) #print statement for total highvalue of Mocha
elif coftype == "L": #If coftype = L
value = (array2zip[2]*4.00) #Formula for high sales of Latte
print("The zipcode with the highest sales is: 48088") #print statement for zipcode
print("Total Sales : $",format(value,',.2f')) #print statement for total highvalue of Latte
elif coftype == "R": #If coftype = R
value = (array3zip[3]*2.50) #Formula for high sales of Regular
print("The zipcode with the highest sales is: 48026") #print statement for zipcode
print("Total Sales : $",format(value,',.2f')) #print statement for total highvalue of Regular
elif coftype == "D": #If coftype = D
value = (array1zip[4]*2.50) #Formula for high sales of Decaf
print("The zipcode with the highest sales is: 48093") #print statement for zipcode
print("Total Sales : $",format(value,',.2f')) #print statement for total highvalue of Decaf
elif coftype == "C": #If coftype = C
value = (array4zip[5]*4.50) #Formula for high sales of Caramel
print("The zipcode with the highest sales is: 48066") #print statement for zipcode
print("Total Sales : $",format(value,',.2f')) #print statement for total highvalue of Caramel
else: #Else statement
print ("Sorry you did not enter M, L, R, D or C - Please Enter One of those letters")#print statement for wrong info
hightype(value) #Re-run hightype function if wrong info was entered
return value #Returns the value variable to other parts of the program

def zipcodesales(totalsales): #zipcodesales function
zipcode = 0 #Variable declaration
zipcode = inputdata(zipcode) #Runs inputdata zipcode function
totalsales=0.0 #Variable declaration
if zipcode == 48093: #If zipcode = 48093
totalsales = (array1zip[1]*3.50)+(array1zip[2]*4.00)+(array1zip[3]*2.50)+(array1zip[4]*2.50)+(array1zip[5]*4.50) #formula for totalvalue of 48093 zipcode
print("The total sales for zipcode 48093 are : $",format(totalsales,',.2f')) #Print statement for 48093 zipcode totalsales
elif zipcode == 48088: #If zipcode = 48088
totalsales = (array2zip[1]*3.50)+(array2zip[2]*4.00)+(array2zip[3]*2.50)+(array2zip[4]*2.50)+(array2zip[5]*4.50) #formula for totalvalue of 48088 zipcode
print("The total sales for zipcode 48088 are : $",format(totalsales,',.2f')) #Print statement for 48088 zipcode totalsales
elif zipcode == 48026: #If zipcode = 48026
totalsales = (array3zip[1]*3.50)+(array3zip[2]*4.00)+(array3zip[3]*2.50)+(array3zip[4]*2.50)+(array3zip[5]*4.50) #formula for totalvalue of 48026 zipcode
print("The total sales for zipcode 48026 are : $",format(totalsales,',.2f')) #Print statement for 48026 zipcode totalsales
elif zipcode == 48066: #If zipcode = 48066
totalsales = (array4zip[1]*3.50)+(array4zip[2]*4.00)+(array4zip[3]*2.50)+(array4zip[4]*2.50)+(array4zip[5]*4.50) #formula for totalvalue of 48066 zipcode
print("The total sales for zipcode 48066 are : $",format(totalsales,',.2f')) #Print statement for 48066 zipcode totalsales
else: #else statement
print("You entered the wrong zipcode, please enter 48093,48088,48026, or 48066")#print statement for wrong info
zipcodesales(totalsales) #reruns zipcodesales function in case of wrong data
return totalsales #Returns totalsales variable to other parts of the program

##Program begins here
welcome() #Runs the welcome function 1 time
while menuselection != "E": #While the menu selection is not E the program will loop and continue to ask for different selections
menuselection = menu(menuselection) #Runs the menuselection function that runs the rest of the program


that one took a while - again i wouldnt use all those comments but they make me in class or i get downgraded, this will fetch 100%
Go Back To General Archive Topic List
Prev12
Add Reply New Topic