By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

int input

P: 3
sorry but this is probably a stupid question but;
if you have an input like
pos = 'c3'
#and you want to take out the second character as an int what do you need to add to stop it from producing an error if the second character is a letter
eg. pos = 'cd'
# i've been using
int(pos[1]) # but it gives an error when the [1] is a letter not a number.
Mar 29 '08 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
sorry but this is probably a stupid question but;
if you have an input like
pos = 'c3'
#and you want to take out the second character as an int what do you need to add to stop it from producing an error if the second character is a letter
eg. pos = 'cd'
# i've been using
int(pos[1]) # but it gives an error when the [1] is a letter not a number.
Following are a couple of ways.
Expand|Select|Wrap|Line Numbers
  1. >>> pos = 'cd'
  2. >>> digits = '0123456789'
  3. >>> if pos[1] in digits:
  4. ...     print "It's a digit!"
  5. ... else:
  6. ...     print "It's no digit!"
  7. ...     
  8. It's no digit!
  9. >>> pos = 'c4'
  10. >>> if pos[1] in digits:
  11. ...     print "It's a digit!"
  12. ... else:
  13. ...     print "It's no digit!"
  14. ...     
  15. It's a digit!
  16. >>> try:
  17. ...     col = int(pos[1])
  18. ... except:
  19. ...     col = pos[1]
  20. ... 
  21. >>> col
  22. 4
  23. >>> pos = 'cd'
  24. >>> try:
  25. ...     col = int(pos[1])
  26. ... except:
  27. ...     col = pos[1]
  28. ...     
  29. >>> col
  30. 'd'
  31. >>> 
Mar 29 '08 #2

P: 3
# heres the problem how can i manipulate just row = to get the aproiate response
position = 'cd'
col = position[0]
row = int(position[1])
if col in ['a','A','b','B','c','C','d','D','e','E','f','F','g ','G','h','H'] and row in [1,2,3,4,5,6,7,8]:
print 'The piece is moved to %s%s.' % (col,row)
elif col not in ['a','A','b','B','c','C','d','D','e','E','f','F','g ','G','h','H'] and row in [1,2,3,4,5,6,7,8]:
print 'The first coordinate is not in the range a-h or A-H!'
elif col in ['a','A','b','B','c','C','d','D','e','E','f','F','g ','G','h','H'] and row not in [1,2,3,4,5,6,7,8]:
print 'The second coordinate is not in the range 1 to 8!'
else:
print 'The column value and row value is not in the ranges a-h, A-H and 1 to 8!'
Mar 29 '08 #3

Smygis
100+
P: 126
Why not use the built in stuff that whas ment for this...

Expand|Select|Wrap|Line Numbers
  1. >>> pos = "cd"
  2. >>> pos[1].isdigit()
  3. False
  4. >>> pos[1].isalpha()
  5. True
  6. >>> pos = "c1"
  7. >>> pos[1].isdigit()
  8. True
  9. >>> pos[1].isalpha()
  10. False
  11. >>> 
  12.  
Mar 29 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.