0

I am learning how to code through this book called "Headfirst Programming", which I am really enjoying so far.

One of the projects in the book uses the following code:

def save_transaction(price, credit_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"))
choice = int(input("choose an option: "))
if choice == option:
   running = false
else: 
   credit_card = input("Credit card number: ")
   save_transaction(prices[choice - 1], credit_card, items[choice - 1])

I can see the logic behind using the "if choice == option then running = false" code (it lets the user add an arbitrary number of items), but this code, as is, runs an infinite loop in the shell. This is strange because I copied it directly from the book and the author is using python 3.0, as am I.

Does anyone have a guess as to why this code runs an infinite loop and how to solve this problem, while keeping the code's core functionality intact?

Thanks

2
  • 7
    Watch your indentation! Commented Dec 29, 2009 at 10:06
  • -1: Copying and pasting the code example incorrectly doesn't lead to a particularly clear or useful kind of question. Commented Dec 29, 2009 at 15:55

3 Answers 3

8

As you've probably read, Python uses indentation to identify blocks of code.

So...

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

will run forever, and

print(str(option) + ". Quit"))
choice = int(input("choose an option: "))
if choice == option:
   running = false
else: 
   credit_card = input("Credit card number: ")
   save_transaction(prices[choice - 1], credit_card, items[choice - 1])

is never reached. Simply fix the indentation and you should be right.

Sign up to request clarification or add additional context in comments.

5 Comments

while (no pun intended) I like python's significant indentation, it must be a pain for book writers.
Why would proper indentation for Python be any harder than proper indentation for any other language?
If the editor of a book on another language messes up the indentation it looks ugly. If the editor of a python book messes up the indentation the meaning of the code is changed. Also books have very limited horizontal space, so not being able to break up lines may make life much harder.
Not true Douglas Leeder - I have seen books use the backslash to escape the end of line and thereby not have to worry about horizontal space
the simple answer to books that have trouble with breaking up lines is to not do long one-liners and keep the examples simple (and therefore the code compact)
0

It's pretty clear that the indentation here is wrong -- since the whole body of the function should be indented relative to the def save_transaction(price, credit_card, description): line.

Hence, I suspect that there's also a problem with the indentation below the while running line, and that the lines which change the value of running should be inside the loop.

Comments

0

You need to indent forward all the lines starting from print(str(option) + ". Quit")). Align them at the same level as for choice in items:.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.