# Introduction to structures
from collections import namedtuple
#Structure defintion
struct = namedtuple("struct", "integer real")
first_structure = struct( 7, 3.14 )
second_structure = first_structure
print "integer = %d, real = %f"%(second_structure.integer,second_structure.real)
# Initializing a structure
from collections import namedtuple
#Structure defintion
struct = namedtuple("struct", "integer real")
structure = struct( 7, 3.14 )
print "integer = %d, real = %f"%(structure.integer,structure.real)
# The structure tag
from collections import namedtuple
#Structure defintion
struct = namedtuple("struct", "integer real")
first_structure = struct( 7, 3.14 )
second_structure = first_structure
print "integer = %d, real = %f"%(second_structure.integer,second_structure.real)
# An array of structures
from collections import namedtuple
#Structure defintion
struct = namedtuple("struct", "code price")
#code = [ "a", "b", "c", "d", "e" ]
#price = [ 3.29, 2.99, 0.59, 1.59, 2.39 ]
table = [ struct("a",3.29) , struct("b",2.99), struct("c",0.59), struct("d",1.59), struct("e",2.39) ]
print "Enter item code:",
item_code = "a"
print item_code
i = 0
while item_code != "." :
#Search for 'item_code' in 'table'
for i in range(4) :
if table[i].code == item_code :
break
if i < 4:
print "Enter quantity: ",
quantity = 1
print quantity
print "\nUnit price = %.2f, total price = %.2f.\n\n"%(table[i].price,quantity * table[i].price)
else :
print "\nItem code %c does not exist.\n\n"%item_code
print "Enter item code:",
item_code = "."
print item_code
print "Thank you."
# A pointer in a structure
from collections import namedtuple
#Structure defintion
struct = namedtuple("struct", "name price")
#code = [ "a", "b", "c", "d", "e" ]
#price = [ 3.29, 2.99, 0.59, 1.59, 2.39 ]
table = [ struct("pickles",2.29) , struct("soda",0.99), struct("yogurt",0.59), struct("bread",1.09), struct("milk",0.69) ]
print "Enter item name:",
item_name = "pickles"
print item_name
i = 0
while item_name != "." :
#Search for 'item_code' in 'table'
for i in range(4) :
if table[i].name == item_name :
break
if i < 4:
print "Enter quantity: ",
quantity = 1
print quantity
print "\nUnit price = %.2f, total price = %.2f.\n\n"%(table[i].price,quantity * table[i].price)
else :
print "\nItem code %c does not exist.\n\n"%item_name
print "Enter item code:",
item_name = "."
print item_name
print "Thank you."
# An array in a structure
from collections import namedtuple
#Structure defintion
struct = namedtuple("struct", "name price")
#code = [ "a", "b", "c", "d", "e" ]
#price = [ 3.29, 2.99, 0.59, 1.59, 2.39 ]
table = [ struct("pickles",2.29) , struct("soda",0.99), struct("yogurt",0.59), struct("bread",1.09), struct("milk",0.69) ]
print "Enter item name:",
item_name = "milk"
print item_name
i = 0
while item_name != "." :
#Search for 'item_code' in 'table'
for i in range(5) :
if table[i].name == item_name :
break
if i < 5:
print "Enter quantity: ",
quantity = 5
print quantity
print "\nUnit price = %.2f, total price = %.2f.\n\n"%(table[i].price,quantity * table[i].price)
else :
print "\nItem code %c does not exist.\n\n"%item_name
print "Enter item code:",
item_name = "."
print item_name
print "Thank you."