I'm looking to do the following in this program:
I want to ask the user to set a range of hours they want to sleep in (from when they go to sleep until they wake up). Then, award points proportional to the amount of hours they slept within that range.
Keep in mind that I started learning Python yesterday and had zero experience with any programming language before that.
Here is what I've got so far:
# Timeslot(TS) of hours slept.
TS_start_sleep = int(raw_input("What time did you go to sleep last night:"))
TS_end_sleep = int(raw_input("What time did you wake up this morning:"))
def sleep_TS(points):
if
Also, here is the first part of my program (in cases it helps show my level of knowledge). This section works just fine (I debugged everything) but please feel free to suggest ways that would make it better / more efficient.
points = 0 # Beginning points.
# SLEEP
# Amount of hours slept.
hrs_slept = int(raw_input("Hours slept today:"))
def sleep_length(points):
if hrs_slept == 8:
points += 50
elif 9 <= hrs_slept <= 17:
points = -10 * hrs_slept + 120
elif 4 <= hrs_slept <= 7:
points = 10 * hrs_slept - 40
elif 0 <= hrs_slept <= 3:
points = 10 * hrs_slept - 50
else:
print "Invalid input. Valid hours are between 0 and 17."
points_sleep_length = points
print "+", points, "points"
sleep_length(points)
Thank you for reading!
Edit: Also, I've been using Codeacademy to learn python up until now. However, I'd also be interested in hearing what else the community recommends for learning python and other programming languages as I am going through the material pretty quickly.
2 Answers 2
I don't see a question here, is there something specific you are stuck with?
One thing you might consider is this: At present it looks like first they input the hours, and then you call your function on whatever they have input. It is not until the end of the function that they are told if the hours are invalid, and then you have to run the program again. It would be better to only proceed to the call once a valid input has been made.
for example:
hrs_slept = input("Hours Slept Today:")
while hrs_slept not in xrange(0, 18):
print "Invalid input. Valid hours are between 0 and 17"
print "Please try again"
hrs_slept = input(Hours Slept Today:")
print "You slept for " + str(hrs_slept) + " hours"
then call your function.
Note that this will only work for whole hours. If you want to allow them to input decimal values of hours you will need to use another strategy.
2 Comments
For your second part, udacity CS101 is excellent resource for starting out in python. You can learn a lot from that course.
input(...)is equivalenteval(raw_input(...)). So if the input is an integer, it'll automatically be converted to an integer. But if the input is not an integer, more or less anything can happen. I wouldn't recommend using it overint(raw_input(...)).