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

Austin March
2,310 PointsIm trying to only accept integers with a value of 1 to 10. Nothing seems to work.
day.py is my code: https://w.trhou.se/n1xdfv5220 I think that my issue is with the list[ ] but im not sure. I want to accept values 1 thru 10, but reject all values like '100', or a letter like 'T'. It is hard to compare values after i ask the question because the value they enter has to be a string, but i want an integer. There are only 1 way i think i can do it:
except only integers and tell the user if they try and input a letter that its invalid. Then filter filter the integer they input to only accept it if its between 1 and 10.
I dont know how to use functions for this purpose, so help would be appreciated. thanks in advance.
4 Answers

Cooper Runstein
11,851 PointsNice work so far! Your problem is your comparision
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
scale = input("On a scale of 1 - 10, how is your day going? ")
if scale == my_list: # if scale (an input) is equal to the *entire* list.
print("Valid!")
exit()
elif scale != my_list # if scale (an input) is not equal to the *entire* list.
print("Invalid!")
print("end of script")
What you want to do is check if scale is in the list:
my_list = ['a', 'b', 'c']
my_letter = 'a'
my_letter == my_list #false
my_letter in my_list #true
The way the iteration short cut in works is similar to doing this:
my_list = ['a', 'b', 'c']
my_letter = 'a'
for letter in my_list:
if my_letter == letter:
return True
return False
Finally, for this case, what you're doing is perfect, but for more complex examples, you'll need to learn how to use try/except blocks. These will allow you to take any input and test to make sure it fits your parameters. Check out https://docs.python.org/3/tutorial/errors.html or part 3 of this course: https://teamtreehouse.com/library/python-basics-3
Hope that helps, and maybe gives a bit of insight into your code. Let me know if I can answer anything left unanswered!

Cooper Runstein
11,851 PointsI'm not sure what you mean about workspaces, but here's whats going on with your code:
for my_number in my_list: #for every number in my list
if str(scale) >= "6": //For the condition that is true, do whatever
print("Happy. CHANGE THIS LATER")
elif str(scale) <= "5":
print("Sad. CHANGE THIS LATER")
else:
print("That's not a valid number! Try again later.")
You don't need the for loop, because your if statement does all the work and doesn't depend on my_list

Austin March
2,310 Pointsi fixed it with another method, and now everything works the way i need it to, except when i enter 10. I think it is with my comparisons with the == and <= and >= or something similar, but i dont know why it says 10 is a sad number. only numbers 5 or under should be sad. link: https://w.trhou.se/nk9ebg8qxn

Cooper Runstein
11,851 PointsI'm on a phone, not a computer, so I haven't tested it yet, but I would image that it has something to do with your handling of types. You're turning scale into an integer, but then comparing to to strings.
int(scale) == '5' #left side is an int, right is a stripg

Austin March
2,310 Pointsfixed the str to int errors. not the issue. must be something else. also, im in school no rush for an answer

Cooper Runstein
11,851 Pointsmy_list = [1,2,3,4,5,6,7,8,9,10]
def test_for_mood(mood): #create a function to store your logic that takes a single input
try:
mood = int(mood) #turn the mood (the function's input) into an integer if possible
except ValueError: #if it isn't we can end the program
print("Nope, that isn't a number")
exit()
if mood in my_list: #check if the number is in the list of valid numbers
if mood >= 6: #If the number is happy
print('Happy')
elif mood <= 5: #if not
print('Sad')
else:
#if the number wasn't in the list of numbers. No need to test for the number bigger than 10, this else catches that.
print('That number is not valid')
#The function ended successfuly
print('Done')
scale = input('how was your day 1-10') #get the number
test_for_mood(scale) #pass the number into your logic function
This is what I would do. I'm not sure how experienced you are with functions, but a function would make your code a lot easier to troubleshoot.
Also, just so you know, you can use the range function (https://docs.python.org/3/library/functions.html#func-range) instead of creating a list of numbers between 1 and 10 like so:
my_list = range(1, 11) #up to, but not including 11
If any of this, either the function part or the range is too much for you to grasp at this point, don't worry about it, just knowing that those options exist will help you out as you're learning in the future. Just let me know if you have any more questions!

Austin March
2,310 Pointslist is easier for this amount of numbers, because i want 5, not like 5.5. thank you. it works now! A little too advanced for me, but im getting there

Cooper Runstein
11,851 PointsActually, unless specified otherwise, range will only print out integers (whole numbers). So:
for x in range(1, 11):
print(x)
#1
#2
#3
...
#10
Cooper Runstein
11,851 PointsCooper Runstein
11,851 PointsAlso to directly answer your question as to how to only accept integers:
Austin March
2,310 PointsAustin March
2,310 Pointshttps://w.trhou.se/soak0s2sia doesnt it works, but it displays the message like 5 times... dont know why. thank you so much! also how do i get that type of workspace? i like the U.I. better