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
Aaron Peter
1,498 Pointshow 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
243,318 PointsI'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.")
Andrey Misikhin
16,529 Pointsif value >= 0 and value <= 100:
####################
if value in range(101):
Aaron Peter
1,498 Pointsi 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%. "
Andrey Misikhin
16,529 Pointsmark = -1
while mark not in range(101):
mark = int(input("Bla bla mark"))
Aaron Peter
1,498 PointsAaron Peter
1,498 Pointsi 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
243,318 PointsSteven Parker
243,318 PointsSo perhaps you might incorporate that test in a loop to retry input:
Aaron Peter
1,498 PointsAaron Peter
1,498 PointsThank you