[Tutor] Finding if a passed variable is within to set parameters
Marc Tompkins
marc.tompkins at gmail.com
Tue Oct 11 00:21:38 CEST 2011
On Mon, Oct 10, 2011 at 3:13 PM, Mike Nickey <mnickey at gmail.com> wrote:
> Hey all,
>
> I'm trying to write a def that has a check to see if the entered
> information is within a pre-determined valid set.
> Below is what I have so far but it doesn't seem to be working properly.
> What I want is to have the user be able to enter 1 through 8 and have the
> information pass fine but anything else would cause the user to repeat the
> process.
> I'm using a while loop to try to do this. The only other thing I can think
> of to do is incorporate a list and see if I can check the list to complete
> this.
>
> Any assistance would be greatly appreciated.
>
> def getUserSkillLvl(GameType): #GameType is an in of 8 or 9 passed to this
> def based on previous input
> temp = 0
> while temp == 0:
> if GameType == 8:
> UserSkillLvl = raw_input("Enter your current skill level: ")
> int(UserSkillLvl)
> 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
>
>
int() is a function, not a statement - it returns a value, but doesn't
change the value you pass in to it. (Generally, things with () at the end
are functions... there are exceptions, but it's a useful rule of thumb.) So
UserSkillLvl is a string when the user enters it - you run int() on it, but
throw away the result - it's still a string a moment later when you try to
see whether it's <=8 or >=1.
You could do
> UserSkillLvl = int(UserSkillLvl)
or
> if ((int(UserSkillLvl) <= 8) and (int(UserSkillLvl) >=1)):
Your choice.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111010/aea96ed4/attachment.html>
More information about the Tutor
mailing list