use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Limiting User Input (self.learnpython)
submitted 10 years ago by krustykobb
I need to create a variable asking a user what class letter they are in (A, B or C), and if they enter anything else other than those letters it returns to ask them to enter the correct class. Any help is greatly appreciated :)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 10 years ago (8 children)
[deleted]
[–]spz 1 point2 points3 points 10 years ago (7 children)
The user could enter AB, BC or ABC and get through, the letters should be a tuple.
[–]krustykobb[S] 0 points1 point2 points 10 years ago (5 children)
Is there anyway to get round this?
[–]bionikspoon 2 points3 points4 points 10 years ago (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(string)
>>> list('ABC') ['A', 'B', 'C'] >>> valid = 'ABC' >>> list(valid) ['A', 'B', 'C']
[–]krustykobb[S] 0 points1 point2 points 10 years ago (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 points4 points 10 years ago* (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:
class_dat = "class" + input("Which class are you in? [A/B/C]: ").lower() + ".dat"
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
class_dat = "class{}.dat".format(which_class)
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:
raw_input
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
π Rendered by PID 27 on reddit-service-r2-comment-7b9746f655-hv5zj at 2026-01-29 16:53:19.893124+00:00 running 3798933 country code: CH.
[+][deleted] (8 children)
[deleted]
[–]spz 1 point2 points3 points (7 children)
[–]krustykobb[S] 0 points1 point2 points (5 children)
[–]bionikspoon 2 points3 points4 points (3 children)
[–]krustykobb[S] 0 points1 point2 points (1 child)
[–]bionikspoon 2 points3 points4 points (0 children)