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 trialMike D
2,792 PointsExpanded practice
Okay, so I did this lesson and I was able to perform the quiz correctly, but I wanted to practice a bit more with if and else statements a bit more just to really drive the basics of it so that it becomes muscle memory. I wanted to use the foundations of this lesson and build something a bit more complex code wise for myself.
The first thing I tasked myself with doing was defining the time variable myself to match up with the store_hours variable already provided, so I did time = range(9, 18)
Secondly, I asked a user for input to set up an appointment. The idea was that if the user entered a time that matched any the above times in store_hours then they'd be prompted with a message stating that that time was available. If not they'd be prompted with a message stating to pick another time
to do this I know I had to use the input() assign available that stored that input and then check to see if that matched up with the hours IN store_hours. For some reason it seems to work except that when I input a value that matches up with the store hours the program seems to run the else print function I provided. Here's the code any help would be greatly apprchiatted as I can't figure out where the bug lies in this.
time = range (9, 18) store_hours = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
print("Welcome, to abcde Design's, to set up an appointment please enter the time you wish to schedule. If we have an available slot this program will let you know.") store_hours1 = input(">>> enter time here ")
if time in store_hours == store_hours1: print ("Awesome, it appears we space at that time open, please contact one of our consultants to schedule an appointment now!") else: print ("That time is not available try another")
store_open = None
store_hours = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
2 Answers
Alexander Davison
65,469 PointsGood job! There's a useless variable that you should get rid.
I don't quite understand why you made a variable called time, because this program is all about checking if the user input is in the store_times. The time variable is useless in this situation. Another thing: ranges aren't the same as lists. So that means this code over here will return...
range(1, 5) == [1, 2, 3, 4, 5]
It will return False! They aren't the same, but it is possible to transfer them to lists like this if you're curious:
list(range(1, 5)) == [1, 2, 3, 4, 5]
Now this code will return True. This returns True because we tranformed the range into a list. Now that you got a little handle on ranges, let's move back to your code. Now that we deleted the variable, we need to change the condition of the if statement. We must change the condition to input in store_hours (sorry but I prefer descriptive variable names like input, and I don't like names like 1of_my_cookys which actually is a horrible name in programming). With all that in mind, let's implement that. Here's the code that should work:
# Over here we make a range from 9 to 18 and transform it into a list.
store_hours = list(range(9, 18))
# We print out our little message over here.
print("Welcome, to abcde Design's, to set up an appointment please enter the time you wish to schedule. If we have an available slot this program will let you know.")
# Now we get user input to see what time the user wants.
input_time = input("Enter the time you'd like to come: ")
# Finally, we check and see if the input is in the store_hours list.
if input_time in store_hours:
print("Awesome, it appears we space at that time open, please contact one of our consultants to schedule an appointment now!")
else:
print("That time is not available, try another.")
Good luck! :)
I hope this helps, Alex
Mike D
2,792 PointsThis was really great! Thanks for the clear explination, and going even further with explaining how I was using the range function improperly in that scenerio, makes perfect sense now and I even learned a new function in list () . Thanks again!
Alexander Davison
65,469 PointsNo problem! :)
Alexander Davison
65,469 PointsCan you give me a best answer so this question won't be in the "Unanswered questions" part of the Community? Thanks! :)
~Alex