473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Python - Preventing simple user Input errors with while loops.

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 

2 1259
dwblas
626 Expert 512MB
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
krec
3
Thanks for your generosity!!! You're a genius :D that was exactly what i was looking for :)
Oct 22 '16 #3

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

Similar topics

8
by: hokiegal99 | last post by:
I don't understand how to use a loop to keep track of user input. Could someone show me how to do what the program below does with a loop? Thnaks! ---------------------------- #Write a...
3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What...
5
by: SPEAKIT | last post by:
I'm sorry to ask what must be a very simple question, but I just can't seem to get this to work. Right now I am using the javascript 'prompt' to enter a photoid and the script that I am using uses...
1
by: Stimp | last post by:
I have an input field, which will ultimately be stored in a database and the contents of which will be output on screen at some stage. ASP.NET has built-in functionality to prevent users inputing...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
6
by: kydavis77 | last post by:
i was wondering if anyone could point me to some good reading about the for and while loops i am trying to write some programs "Exercise 1 Write a program that continually reads in numbers...
1
by: jiggaman828 | last post by:
Am I doing something wrong with setting my width? I thought I had it correct. Can any inform me on how to write 4 different while or do-while loops counting down to 0, it has to be lined up column...
4
by: Tarique | last post by:
Hello All. Is the following good enough to be a safe user input routine? What else should i do to improve it ? #include<stdio.h> #include<stdlib.h> #include<string.h> #define ARRAYSIZE 100 ...
8
by: blazzer | last post by:
Hi, I'm beginner at Java. I'm confused with my simple program of using while loops int i = 1; while (i <= input){ System.out.println("Enter description of product #" + i + "...
1
by: newbie93 | last post by:
Hi experts, I am working with a table in a database (mysql) that keeps track of the users. The table fields are id, privilege (ie person's position in a committee -- a number mapping to a position...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.