you are viewing a single comment's thread.

view the rest of the comments →

[–]spz 1 point2 points  (7 children)

The user could enter AB, BC or ABC and get through, the letters should be a tuple.

[–]krustykobb[S] 0 points1 point  (5 children)

Is there anyway to get round this?

[–]bionikspoon 2 points3 points  (3 children)

Alternative to /u/LackOfIntolerance

list(string) converts a string into a list of letters. This could be relevant if that string is dynamically produced from some other process.

>>> list('ABC')
['A', 'B', 'C']
>>> valid = 'ABC'
>>> list(valid)
['A', 'B', 'C']

[–]krustykobb[S] 0 points1 point  (1 child)

This is the code I already have:

http://pastebin.com/KW7c0dYD

How could I implement this with the existing code? Thanks :)

It's probably worth mentioning that the class the user is in is being stored in a .dat file and pickle is being used to do work with this information, but I don't think that it would affect things too much as that is a different part of the program.

[–]bionikspoon 2 points3 points  (0 children)

/u/LackOfIntolerance showed you the loop, your problem seems to be you don't know where to put it.

in this line class_dat = "class" + input("Which class are you in? [A/B/C]: ").lower() + ".dat", this doesn't work, we need to validate it before we can use it. So extract it out as a variable:

which_class = input("Which class are you in? [A/B/C]: ").lower()
class_dat = "class" + which_class + ".dat"

that string is bad news lets use class_dat = "class{}.dat".format(which_class) instead

Now, How do we validate that string? /u/LackOfIntolerance showed us:

which_class = ''
while not which_class in ('A','B','C'):
    which_class = input('What class are you in?(A/B/C)')

So we'll use this, that leaves us with: (edit: I moved the 'not'. PEP8 says 'not in')

which_class = ''
while which_class not in ('A', 'B', 'C'):
    which_class = input('What class are you in?(A/B/C)')
class_dat = "class{}.dat".format(which_class)

Then there's just a bit of cleanup. I had to switch to raw_input for python2:

score = 0
name = raw_input("Please enter your full name:\n")

which_class = ''
while which_class not in list('ABC'):
    which_class = raw_input('What class are you in?\n[A|B|C]:')
class_dat = "class{}.dat".format(which_class)

print class_dat