On 25/02/2012 15:37, Joel Goldstick wrote:
On Sat, Feb 25, 2012 at 5:57 AM, Mike Nickey<mnic...@gmail.com> wrote:
I think I found the issue. It seems that the UserOppSkillLvl wasn't
passing the integer version of this variable back.
Changing line 97 from
return UserOppSkillLvl
to
return int(UserOppSkillLvl)
seems to have helped.
Any suggestions on how to implement the UserSkillLvl section so it's
not redundant code?
On Sat, Feb 25, 2012 at 02:07, Mike Nickey<mnic...@gmail.com> wrote:
Hey all,
I'm trying to wok on a game tracker for my friends. What I have here
partly works but there are areas that I want to change and some areas
that are just not working for me.
The areas that I am having difficulty with are the def pointsNeeded
and oppPointsNeeded.
What is strange to me is that I had pointsNeeded almost exactly as
oppPointsNeeded and only pointsNeeded worked.
I'd like to change this to one function since it does the same thing.
I've tried using a dictionary but that does not seem to be working
well either.
Any advice would be great.
Thanks in advance.
CODE
#===============================================================================
# Get users name -- DONE
# Get Users opponent name DONE
# Get user opponents APA number --DONE
# get users APA number -- DONE
# Determine if this is 8-ball or 9-ball -- DONE
# create number of points needed to win for 9-ball
# create number of games needed to win for 8-ball
# Get Users skill level -- DONE
# get users current opponent skill level -- DONE
# get number of innings played
# number of defenses played
# record who won and lost
#===============================================================================
import __builtin__
import string
pointDictionary = {1:14, 2:19, 3:25, 4:31, 5:38, 6:46, 7:55, 8:65, 9:75}
def getUserName():
return raw_input("Enter your name: ")
def getUserOppName():
return raw_input ("Enter your opponents name: ")
def getUserNumber():
UserID = raw_input("Enter your APA ID number: ")
while (str.isdigit(UserID)==False):
UserID = raw_input("That is not a proper ID. Please re-enter
your APA number: ")
return UserID
def getUserOppNumber():
OppUserID = raw_input("Enter your opponents APA ID number: ")
while (str.isdigit(OppUserID)==False):
UserID = raw_input("That is not a proper ID. Please re-enter
your opponents APA number: ")
return OppUserID
def getGameType():
temp = 0
while temp == 0:
GameType = raw_input("Are you playing 8-ball or 9-ball? ")
if GameType == "8":
print "Good luck and don't get an early 8"
GameType = 8
temp = 1
elif GameType == "9":
print "Good luck! Let's hope you sink the stripe on the break!"
GameType = 9
temp = 1
else:
print "That's not a valid entry, Please try again.: "
return int(GameType)
def CheckRange8Ball(GameType, min=1, max=8):
if not min<= GameType<= max:
raise ValueError('Value out of range')
def getUserSkillLvl(GameType):
if GameType == 8:
UserSkillLvl = prompt = "Enter your current 8-ball skill level: "
This is kind of odd looking. Istead try this:
prompt = "Enter your current %d-ball skill level: " % prompt
Look at http://docs.python.org/release/2.5.2/lib/typesseq-strings.html
or just google python string formatting
Then you can get rid of this below
elif GameType == 9:
UserSkillLvl = prompt = "Enter your current 9-ball level:"
UserSkillLvl = raw_input(prompt)
UserSkillLvl = int(UserSkillLvl)
Now, below. It looks like if GameType is 9, SkillLvl is from 1 to 9.
If GameType is 8, SkillLvl is from 1 to 8.
Your elif statements are complicated. Since the if case succeeds for
Lvl from 1 to GameType, the elif is redundant. You need the else, but
else will always be true if the 'if' code failed.
So simplify elif to else with no condition.
Notice that your logic is identical except for the test dependant on
the game. That logic identical except for the upper range -- if game
8 then 8, if game 9 then 9.
So, try to clean this up and figure a way to just test once, depending
on the game.
To go further you don't need all those round brackets and you can chain
comparisons in Python so this should work.
if 1 <= UserSkillLvl <= GameType:
if GameType == 9:
temp = 0
while temp == 0:
if ((UserSkillLvl<= 9) and (UserSkillLvl>=1)):
print "Thank You"
temp = 1
break
elif ((UserSkillLvl>9) or (UserSkillLvl< 1)):
while temp == 0:
UserSkillLvl = raw_input("Please re-enter your
9-ball skill level")
return UserSkillLvl
if GameType == 8:
temp = 0
while temp == 0:
if ((UserSkillLvl<= 8) and (UserSkillLvl>=1)):
print "thank you"
temp = 1
break
elif (UserSkillLvl>8) or (UserSkillLvl< 1):
while temp == 0:
UserSkillLvl = raw_input("Please re-enter your
skill level: ")
return UserSkillLvl
def getUserOppSkillLvl():
UserOppSkillLvl = raw_input("Enter your opponents current skill level: ")
while (str.isdigit(UserOppSkillLvl)==False):
UserOppSkillLvl = raw_input("That is not a proper Skill Level. \
Please enter a number between 1 and 9 for 9-ball or 1 and 8
for 8-ball: ")
UserOppSkillLvl = int(UserOppSkillLvl)
return UserOppSkillLvl
def getPointsNeeded():
if (GameType == 9):
for UserSkillLvl in range (0, len(pointDictionary)):
pointsNeeded = pointDictionary(UserSkillLvl)
return pointsNeeded
getPointsNeeded will never work as :-
a) you can't call a dictionary, i.e. pointsNeeded =
pointDictionary(UserSkillLvl) is incorrect
b) if you write pointsNeeded = pointDictionary[UserSkillLvl] instead
you'll get a KeyError
c) if GameType isn't 9 you'll get an UnboundLocalError
How do I know this? I've tried it at the interactive prompt, one of
Python's great strengths.
>>> pointDictionary = {1:14, 2:19, 3:25, 4:31, 5:38, 6:46, 7:55, 8:65,
9:75}
>>> def getPointsNeeded():
if (GameType == 9):
for UserSkillLvl in range (0, len(pointDictionary)):
pointsNeeded = pointDictionary(UserSkillLvl)
return pointsNeeded
>>> GameType = 9
>>> pointsNeeded = getPointsNeeded()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<interactive input>", line 4, in getPointsNeeded
TypeError: 'dict' object is not callable!
>>> def getPointsNeeded():
... if GameType == 9:
... for UserSkillLvl in range (0, len(pointDictionary)):
... pointsNeeded = pointDictionary[UserSkillLvl]
... return pointsNeeded
...
>>> pointsNeeded = getPointsNeeded()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<interactive input>", line 4, in getPointsNeeded
KeyError: 0
>>> GameType = 8
>>> pointsNeeded = getPointsNeeded()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<interactive input>", line 6, in getPointsNeeded
UnboundLocalError: local variable 'pointsNeeded' referenced before
assignment
But this function isn't needed anyway just have.
pointsNeeded = pointDictionary[UserSkillLvl]
Hence.
>>> UserSkillLvl = 5
>>> pointsNeeded = pointDictionary[UserSkillLvl]
>>> pointsNeeded
38
def getOppPointsNeeded():
if (GameType == 9):
if (UserOppSkillLvl == 9):
oppPointsNeeded = 75
elif (UserOppSkillLvl == 8):
oppPointsNeeded = 65
elif (UserOppSkillLvl == 7):
oppPointsNeeded = 55
elif(UserOppSkillLvl == 6):
oppPointsNeeded = 46
elif (UserOppSkillLvl == 5):
oppPointsNeeded = 38
elif (UserOppSkillLvl == 4):
oppPointsNeeded = 31
elif (UserOppSkillLvl == 3):
oppPointsNeeded = 25
elif (UserOppSkillLvl == 2):
oppPointsNeeded = 19
elif (UserOppSkillLvl == 1):
oppPointsNeeded = 14
return oppPointsNeeded
UserName = getUserName()
UserOppName = getUserOppName()
UserID = getUserNumber()
OppUserID = getUserOppNumber()
GameType = getGameType()
UserSkillLvl = getUserSkillLvl(GameType)
UserOppSkillLvl = getUserOppSkillLvl()
print "\nPlayer Name:",UserName, "\nOpponent Name:", UserOppName,
"\nUser ID: ",UserID, "\nOpponent APA ID", OppUserID, \
"\nGameType: ",GameType,"\nUser Skill Level: ",UserSkillLvl, "\nUser
Opponents Level: ",UserOppSkillLvl
pointsNeeded = getPointsNeeded()
oppPointsNeeded = getOppPointsNeeded()
print "\nYou need", pointsNeeded, "to win while your opponent needs",
oppPointsNeeded,"."
--
~MEN
--
~MEN
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
--
Cheers.
Mark Lawrence.
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor