First: Please use the [CODE] button when posting code. It does all good things, no bad things, AND it maintains indentation, which is critical for Python.
Second: I would use a dictionary like this:
items = {
'd': ("Dagger", 10),
's': ("Sword", 50),
# and so forth
}
# and later when looking up the response
choice = raw_input(prompt).lower()[0]
if not choice in items:
# boo, hiss
print(formatstring%items[choice])
I don't see why the loop doesn't break, but without indentation, it is very hard to be sure.
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
your line 35 is incorrectly indented, though Python 2.7 for some odd reason accepts it as syntactically correct. I don't see why the loop doesn't break: My little test case worked ok.
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
well of course I did not write a complete program, and so I never defined the variable prompt="You may buy a Dagger, Sword, Bow or Armor, or 'z' to quit: " nor the variable formatstring which will look something like "You bought %s for %d gold"
In Python 3.x you will need to change to call input(prompt) rather than raw_input(prompt) You will also need to treat print as a function instead of an operator. The rest should work as expected.
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256