Jump to content


Sunny219

Member Since 26 Jun 2014
Offline Last Active Aug 17 2014 06:02 PM
-----

Topics I've Started

Chapter6 P184

16 August 2014 - 06:24 PM

Hi Guys,


I follow head first programming book. When I get to p184, I got stuck. I tried to write the coffee bar POS program as the book suggested. I got something wrong.
When I ran the code, the IDLE showed that File "/Users/.../Documents/Headfirstprogramming/chapter6/pos1.py", line 26, in <module> save_transaction(prices[chioce - 1],credit_card, items[choice - 1])
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Please help me out! Thanks a lot!!!

The below is the codes:

def save_transaction(price, crdit_card, description):
file=open('transactions.txt','a')
file.write("%s%07d%s\n"%(credit_card, price*100,description))
file.close()

items=['DONUT', 'LATTE','FILTER','MUFFIN']
prices=[1.50, 2.0, 1.80, 1.20]
running=True

while running:
option=1
for choice in items:
print(str(option)+ '.'+choice)

option=option+1
print(str(option)+'. Quit')

chioce= int(input('Choose an option: '))

if chioce==option:
running=False

else:
credit_card=input('Credit card number: ')

save_transaction(prices[chioce - 1],credit_card, items[choice - 1])

Pickle Module

26 June 2014 - 04:06 AM

Hi guys,

When I learn Head First Python book to p134 . Here is the code:

import pickle

try:
with open('man_data.txt','wb') as man_file:
pickle.dump(man, man_file)
with open('other_data.txt', 'wb') as other_file:
pickle.dump(other, other_file)

except IOError as err:
print('File error: '+str(err))

except pickle.PickleError as perr:
print('Pickling error: '+str(perr))

however, when I run the code, something is wrong like this:

>>>
Traceback (most recent call last):
File "/Users/shijun/Documents/HeadFirstPython/chapter4/sketch10.py", line 5, in <module>
pickle.dump(man, man_file)
NameError: name 'man' is not defined.

Can you help me out? Thanks a lot.