3

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.

6
  • Don't use int(raw_input("prompt")), just use input('prompt') Commented Jul 4, 2013 at 5:55
  • What is the difference? Does using 'input' automatically make the input into an integer? Commented Jul 4, 2013 at 6:04
  • more appropriate at http://codereview.stackexchange.com/questions/ask Commented Jul 4, 2013 at 6:07
  • @user2549082 input(...) is equivalent eval(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 over int(raw_input(...)). Commented Jul 4, 2013 at 6:07
  • Regarding learning material: the official Python site has a nice tutorial: docs.python.org/2/tutorial Commented Jul 4, 2013 at 6:17

2 Answers 2

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.

answered Jul 4, 2013 at 6:16
Sign up to request clarification or add additional context in comments.

2 Comments

My question was most of a "how do I even approach the following..." kind of question. This is still very useful to know though; I will definitely try to implement it in my code.
seems like you are on the right track. List objects and while loops might be useful in these preliminary steps.
1

For your second part, udacity CS101 is excellent resource for starting out in python. You can learn a lot from that course.

answered Jul 4, 2013 at 7:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.