954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

A test for input

Hi all,

I am new Phython user and started using Python 2.7 just a couple days ago. I have written this code which is working, But when user put in correct code loop is not breaking ?? just wondering if it could be improved and what book would you recommend for Python 2.7 or 3? and do you know when Python 3 will be available? also hear about something called gygame (I think) which should work with python but I could not get this work with Python ! any help here would greatful recieved..

INPUT TEST>>>PYTHON 2.7: DATE OF CODING 11.12.2010


#set up verible for shopping

item = ['Dagger', 'Sword', 'Bow', 'Armour']
price = [10, 50, 35, 75]
gold=100

# display items for sale

print ' I have the following items for sale'
print
print item[0] + ' at ' + str(price[0]) + ' Gold bits'
print
print item[1] + ' at ' + str(price[1]) + ' Gold bits'
print
print item[2] + ' at ' + str(price[2]) + ' Gold bits'
print
print item[3] + ' at ' + str(price[3]) + ' Gold bits'


#buying a item and deducting money
while True:

input = raw_input("\nPress, D, S, B or A to purchuse the item or Z to leave: ")
if input == 'd' or input =='D':
print "\nYou broght a " + item[0] + ' at ' + str(price[0])
elif input =='s' or input =='S':
print "\nYou broght a " + item[1] + ' at ' + str(price[1])
elif input =='b' or input =='B':
print "\nYou broght a " + item[2] + ' at ' + str(price[2])
elif input =='a' or input =='A':
print "\nYou broght a " + item[3] + ' at ' + str(price[3])
elif input =='z' or input == 'Z':
print '\nBye for now'
break;
else:
print '\nYou need to Enter D, S, B, A OR Z. TRY AGAIN!'

print "\nYou have Gold " + str(gold-price[0]) + ' bits left'

Thanks for you time

Stephen

FAITH2011
Light Poster
29 posts since Dec 2010
Reputation Points: 10
Solved Threads: 2
 

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
 

Hi griswolf,

Thankyou for you kind reply, sorry I did put the code in correctly it my first day on here and still unsure how this site works. I will try you code enter tomorrow night and get back to you. Once again thank you for you help

Stephen

FAITH2011
Light Poster
29 posts since Dec 2010
Reputation Points: 10
Solved Threads: 2
 

i will wait till you put your code in the code tag tomorrow before i comment.
;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

Here the code in right format I hope, Sorry about earlier code but it is my first day

#set up verible for shopping

item = ['Dagger', 'Sword', 'Bow', 'Armour']
price = [10, 50, 35, 75]
gold=100

# display items for sale

print ' I have the following items for sale'
print
print item[0] + ' at ' + str(price[0]) + ' Gold bits'
print
print item[1] + ' at ' + str(price[1]) + ' Gold bits'
print
print item[2] + ' at ' + str(price[2]) + ' Gold bits'
print
print item[3] + ' at ' + str(price[3]) + ' Gold bits'


#buying a item and deducting money
while True:
   
   input = raw_input("\nPress, D, S, B or A to purchuse the item or Z to leave: ")
   if input == 'd' or input =='D':
      print "\nYou broght a " + item[0] + ' at ' +  str(price[0])
   elif input =='s' or input =='S':
      print "\nYou broght a " + item[1] + ' at ' +  str(price[1])
   elif input =='b' or input =='B':
      print "\nYou broght a " + item[2] + ' at ' +  str(price[2])
   elif input =='a' or input =='A':
      print "\nYou broght a " + item[3] + ' at ' +  str(price[3])
   elif input =='z' or input == 'Z':
       print '\nBye for now'
       break;
else:
   print '\nYou need to Enter D, S, B, A OR Z.   TRY AGAIN!'

print "\nYou have Gold " + str(gold-price[0]) + ' bits left'
FAITH2011
Light Poster
29 posts since Dec 2010
Reputation Points: 10
Solved Threads: 2
 

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
 

As griswolf pointed to you about you using dict to save your item values its one of the best advice for you.

Your data are more static and moreoevr you have paired them in a staic way which you pull the results from the lists. Its not efficient. You need a key and value stuff, a hash data type which is a dict in python.

Besides you code loop works fine with me.

Work on that.. ;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

Thanks for the advice and help. I will try this out.

FAITH2011
Light Poster
29 posts since Dec 2010
Reputation Points: 10
Solved Threads: 2
 

Hi all,

Tried the code above supplied by giswolf and got an error message, not saying there is anything wrong with coding just my lack of knowledge with Python, I expect. Keep saying prompt is not defined. Well I need to learn more Now using Python 3.1.3. so please be gentle with me as I know very little about Phyton, But I am learning.. Thank you

FAITH2011
Light Poster
29 posts since Dec 2010
Reputation Points: 10
Solved Threads: 2
 

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
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: