By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,757 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,757 IT Pros & Developers. It's quick & easy.

Python - Preventing simple user Input errors with while loops.

P: 3
Hello everyone! Anticipating my thanks for reading. Well, on line 2 i was trying to prevent an user of inputing something different than 1,2,3,4,5,6. It works, yes, but...
I was wondering if i could make it more simple. I first tried to do range(1,7) but it didn't work because it creates a list with [1,2,3,4,5,6] and not integers.
I was wondering if someone could help me with this! Thank you once again


Expand|Select|Wrap|Line Numbers
  1. s_test_n = int(raw_input("How many tests did your student do?[1-6]:"))
  2.         while s_test_n != 1 and s_test_n != 2 and s_test_n != 3 and s_test_n != 4 and s_test_n != 5 and s_test_n != 6:
  3.             s_test_n = int(raw_input("Sorry but, how many tests did your student do?[1-6]:"))
Oct 22 '16 #1

✓ answered by dwblas

Using a list/range is the common way as it can include/accept anything you want, not just a range of numbers.
Expand|Select|Wrap|Line Numbers
  1. s_test_n=0
  2. include_list=range(1, 7)
  3. while s_test_n not in include_list:
  4.     s_test_n=int(raw_input("How many tests did your student do?[1-6]:"))
  5. ##
  6. ##  or
  7. ##    while s_test_n < 1 or s_test_n > 6 

Share this Question
Share on Google+
2 Replies


Expert 100+
P: 619
Using a list/range is the common way as it can include/accept anything you want, not just a range of numbers.
Expand|Select|Wrap|Line Numbers
  1. s_test_n=0
  2. include_list=range(1, 7)
  3. while s_test_n not in include_list:
  4.     s_test_n=int(raw_input("How many tests did your student do?[1-6]:"))
  5. ##
  6. ##  or
  7. ##    while s_test_n < 1 or s_test_n > 6 
Oct 22 '16 #2

P: 3
Thanks for your generosity!!! You're a genius :D that was exactly what i was looking for :)
Oct 22 '16 #3

Post your reply

Sign in to post your reply or Sign up for a free account.