Jump to content


Photo

Chapter6 P184


  • Please log in to reply
2 replies to this topic

#1 Sunny219

Sunny219

    New Member

  • Members
  • Pip
  • 4 posts

Posted 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])

#2 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 17 August 2014 - 02:22 AM

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])


Can you please post properly formatted and indented code. :(

You just have to use


[code]
Paste your code here.
[/code]


or use <> button on the editor.

See below for problems.

def save_transaction(price, crdit_card, description): #change "crdit_card" to "credit_card"; else you may get another error too.
    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: ')) # New variable "chioce"
    
    if chioce==option: # The new variable used here
        running=False
    
    else:
        credit_card=input('Credit card number: ')

        save_transaction(prices[chioce - 1],credit_card, items[choice - 1]) #And PROBLEM HERE
In the last line; Are "chioce" and "choice" the same?! Better change last "choice" to "chioce" as you have used it as a new variable here but "choice" is assigned the "items" and you did not override the value by using a new variable "chioce". So, the problem when using "choice" in the last line as "choice" still store a string value from "items". :) (Although all this because of one spelling mistake :P )

#3 Sunny219

Sunny219

    New Member

  • Members
  • Pip
  • 4 posts

Posted 17 August 2014 - 04:18 PM

Hi #TM#,

Thanks so much. You help me solve the problem. I had the spelling mistake which caused that error.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users