Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

how do you validate a value in the range of 0-100 ? (in python)

how do you validate a value in the range of 0-100 ? (in python)

4 Answers

Steven Parker
Steven Parker
243,318 Points

I'm not sure what you mean by "validate", perhaps if you shared the code where the validation will be performed and/or a link to the page you are working with?

But it sounds like you may just need to perform a test. So, assuming your value was stored in a variable named "val":

if val < 0 or val > 100:
    print("The value is out of range.")

i was given a task , Task: 'Start a new Python script and write some suitable input commands to store three items of data into three variables: Student Name, Coursework Mark, and Exam Mark. The inputs for coursework mark and exam mark should be validated as a value in the range 0-100%.', i didnt know how to do this part , "The inputs for coursework mark and exam mark should be validated as a value in the range 0-100%. "

Steven Parker
Steven Parker
243,318 Points

So perhaps you might incorporate that test in a loop to retry input:

while True:
    coursework = int(input("Enter the Coursework Mark: "))
    if coursework < 0 or coursework > 100:
        print("The value is out of range, try again.")
    else:
        break

Thank you

if value >= 0 and value <= 100:
####################
if value in range(101):

i was given a task , Task: 'Start a new Python script and write some suitable input commands to store three items of data into three variables: Student Name, Coursework Mark, and Exam Mark. The inputs for coursework mark and exam mark should be validated as a value in the range 0-100%.', i didnt know how to do this part , "The inputs for coursework mark and exam mark should be validated as a value in the range 0-100%. "

mark = -1
while mark not in range(101):
    mark = int(input("Bla bla mark"))