Chapter 13, Structures

Program 13-1, Page Number: 370

In [1]:
# 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)
integer = 7, real = 3.140000

Program 13-2, Page Number: 371

In [2]:
# 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)
integer = 7, real = 3.140000

Program 13-3, Page Number: 372

In [3]:
# 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)
integer = 7, real = 3.140000

Program 13-4, Page Number: 375

In [4]:
# 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."
Enter item code: a
Enter quantity:  1

Unit price = 3.29, total price = 3.29.


Enter item code: .
Thank you.

Program 13-5, Page Number: 378

In [5]:
# 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."
Enter item name: pickles
Enter quantity:  1

Unit price = 2.29, total price = 2.29.


Enter item code: .
Thank you.

Program 13-6, Page Number: 381

In [6]:
# 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."
Enter item name: milk
Enter quantity:  5

Unit price = 0.69, total price = 3.45.


Enter item code: .
Thank you.