Jump to content


Photo

Confusion In Chapter 6 183-184


  • Please log in to reply
1 reply to this topic

#1 johans

johans

    New Member

  • Members
  • Pip
  • 2 posts

Posted 27 April 2015 - 05:28 PM

Hello I'm having a bit difficulty comprehending this pre-made function 

save_transaction

 

Where in the later code is it getting the "credit_card", "price * 100", and "description" and placing them in their proper locations

 

Also the picture in the book shows a 7+  pad or space in between the description and the price.

 

63827462387648320000220        LATTE

 

I don't think the pre-made function is doing this as well. Am I wrong?
 

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

itens = ["DONUTS", "LATTE", "FILTER", "MUFFIN"]
price = [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")
     choice = int(input("Choose and option: "))
     if choice == option:
          running = False
     else:
          credit_card = input("Credit card number; ")
          save_transaction(prices[choice - 1], credit_card, items[choice - 1]  

Also thank you in advance, sorry for any mistakes made here

 



#2 johans

johans

    New Member

  • Members
  • Pip
  • 2 posts

Posted 30 April 2015 - 11:16 AM

As I continued I did figure out the answer to my questions. And since as far as I can see there's no way to deleting post on here. I might as well update it in case some one so happens to get to this post with the same question.

 

On the first question it was a silly over thought. The answer was indeed right there in front of me

def save_transaction(price, credit_card, description): 
# ^^ : credit_card , price, and description defined here

     file = open("transaction.txt", "a")
     file.write("%s%07d%s\n % (credit_card, price * 100, description)) 
#    ^^ : Later placed in their proper order
     file.close()

     #Lines of code
     #Lines of code
     #After the else statement

save_transaction(prices[choice - 1], credit_card, items[choice - 1])  
# ^^ : called the function here
 As for the second question. I was indeed correct. It saves the file in this way:
 
63827462387648320000220LATTE
 
Instead of this  way 
 
63827462387648320000220        LATTE
 
The book later corrects it by using this using this code
 file.write("%16s%07d%16s\n % (credit_card, price * 100, description))

Edited by johans, 30 April 2015 - 11:21 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users