LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2008, 09:41 PM   #1
bandana_pig
LQ Newbie
 
Registered: Aug 2008
Posts: 11

Rep: Reputation: 0
Python-Base 10 error (newbie)


Hi all,

I'm having a bit of trouble with a program I'm writing. I had first written it using array comparisons, but figured it would be better written as shown below.

To explain, it's a look-up utility for a reference chart with a game I play. The way it should work in a nutshell is:
if defn is lower than att, print 3
if defn is equal to or up to 2 * att, print 4
if defn is > 2 * att, print 5

Everything seems to work fine until I pass the value of 10 to att. In that instance, it passes through to the end of the if statement and returns a default. I'm thinking it might have something to do with the error I get when I pass no value to either variable when running the program:
Quote:
File "./test.py", line 15, in <module>
if defn <= (int(att) * 2) + 1:
ValueError: invalid literal for int() with base 10: ''
I'm kind of confused here. . .

Code:
#!/usr/bin/python
#
#  Test run for the toHit scripts without referencing arrays.
print """
Enter the attacker and defender weapon skills when prompted.  If you'd like to leave the program at any time, type 'exit.'
"""

while 1:
	att = raw_input("Enter the Attacker's Weapon Skill: ")
	if att == 'exit':break
	defn = raw_input("Enter the Defnder's Weapon Skill: ")
	if defn == 'exit':break
	userOutput = "To hit you will need to roll a "
	
	if  defn <= (int(att) * 2) + 1:
		print userOutput + "5 or better. "
	elif defn == range(int(att), int(att * 2) + 1):
		print userOutput + "4 or better. "
	elif defn < att:
		print userOutput + "3 or better. "
	else:
		print "Comparison not found.  Please check your numbers and try again. "

print """
Run ended.  You've either entered a value for a WS that was not correct or you ended the program by typing 'exit.'
"""
This may not be the best way to write this, but I've just started (programming altogether) and am trying to work out different approaches to the same objective.

Any tips would be greatly appreciated, and not just those limited to the expressed problem I'm having.


Thanks!
 
Old 11-07-2008, 04:38 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Perhaps the following code can give you some ideas and insights? Try replacing your if block with it.
Code:
print type(defn), type(att)
att = int(att)
defn = int(defn)
print type(defn), type(att)
if defn < att: print 3
elif defn <= att*2: print 4
else: print 5
If it doesn't make sense to you, tell me what you've been thinking about when trying to use and modify this code, and I'll explain it to you in more detail.

One problem is that you're comparing strings instead of ints. In python, strings are compared alphabetically, so "10" comes before "2", because the character "1" comes before the character "2". The numbers, as characters, occur in their natural order. See also asciitable.com, or ascii(1).

Also, you may want to do
Code:
try: att = raw_input("blah: ")
except EOFError: break
That way, when someone hits ^D your program exits normally instead of with an error message, which is good form on *nix [try ^D-closing cat, grep, sed, ...].

Also also, you don't have to tell the user that your program terminated; he can tell by his shell prompt appearing again. Compare with cat and others again

Also also also, try:
Code:
print 5 - (defn <= 2*att) - (defn < att)
Here's the explanation of the magic:
Code:
print int(defn <= 2*att), int(defn < att)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
no python module base.g? hcgernhardt Linux - Software 2 04-24-2018 09:03 AM
my apt-get does not work :(: "dpkg: error processing python-kaa-base (--configure):" frenchn00b Debian 3 04-22-2008 03:30 PM
base-config error Joe Soap Debian 3 01-12-2004 09:45 AM
Base Installation Error dingo77 Debian 6 07-25-2003 02:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration