Author |
Message |
mms6
|
Posted: Sat Sep 26, 2009 9:31 pm Post subject: Python HELP |
|
|
Basically the question asks :
--------------------------------------
Write the definition of a function typing_speed , that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that person in words per minute (a float ).
And my code is:
---------------------------
def typing_speed (num, time):
num >= 0
time > 0
time_min = time / 60
speed = float (num / time_min)
return speed
But for some reason it is still wrong. What's wrong with the code? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
andrew.
|
Posted: Sat Sep 26, 2009 9:42 pm Post subject: RE:Python HELP |
|
|
For some reason, time_min is getting a value of 0. Your code works though if you take it out and do this instead:
Python: |
def typing_speed (num, time):
num >= 0
time > 0
speed = float (60*num/time)
return speed
print typing_speed (5, 10) |
Update: Also, casting time_min as float doesn't do anything either. I really can't figure out why it's not working (it's probably something simple and stupid). If you or anybody has figured it out, please post the solution here as I am interested in seeing it. |
|
|
|
|
|
DemonWasp
|
Posted: Sat Sep 26, 2009 11:30 pm Post subject: RE:Python HELP |
|
|
I don't know python, but usually (int) / (int) returns an int, so you're probably getting time_min == 10 / 60 == 0. Try this instead:
time_min = time / 60.0
The .0 on the end changes it to a floating-point number, which means the division is float-division instead of int-division.
This is me speaking from C/C++/Java land though so I could easily be wrong. |
|
|
|
|
|
rdrake
|
Posted: Sat Sep 26, 2009 11:46 pm Post subject: Re: RE:Python HELP |
|
|
DemonWasp @ Sat Sep 26, 2009 11:30 pm wrote: I don't know python, but usually (int) / (int) returns an int, so you're probably getting time_min == 10 / 60 == 0. Try this instead:
time_min = time / 60.0
The .0 on the end changes it to a floating-point number, which means the division is float-division instead of int-division.
This is me speaking from C/C++/Java land though so I could easily be wrong. Winner! Andrew, you are using float() incorrectly in this particular case.
Try this:
Python: | def typing_speed (num, time):
num >= 0
time > 0
speed = float (60)*num/time
return speed
print typing_speed (5, 10) | Or just use 60.0 as the post above me says. |
|
|
|
|
|
mms6
|
Posted: Sun Sep 27, 2009 2:54 am Post subject: RE:Python HELP |
|
|
Thanks I really appreciate the help from nice people
and for those who are wondering rdrake's solution is one of the correct answers. |
|
|
|
|
|
mms6
|
Posted: Sun Sep 27, 2009 3:18 am Post subject: RE:Python HELP |
|
|
The other question is similar, but I keep on getting the code not to work properly
Write the definition of a function words_typed , that receives two parameters. The first is a person's typing speed in words per minute (an int greater than or equal to zero). The second is a time interval in seconds (an int greater than zero). The function returns the number of words (an int ) that a person with that typing speed would type in that time interval.
My code is:
---------------------------------
def words_typed (typespeed, time):
typespeed >= 0
time > 0
numberofwords = typespeed * (time/60)
return numberofwords |
|
|
|
|
|
[Gandalf]
|
Posted: Sun Sep 27, 2009 4:22 am Post subject: RE:Python HELP |
|
|
code: | numberofwords = typespeed * (time / 60.0) |
You need to ensure you're working with floats. One way of doing this is keeping one of the operands a float (ie. 60.0 rather than 60). |
|
|
|
|
|
andrew.
|
Posted: Sun Sep 27, 2009 9:20 am Post subject: RE:Python HELP |
|
|
Oh, I don't really know how to use float() in Python. I just assumed it was correct because that's how it originally was and it seemed to compile and run. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
mms6
|
Posted: Sun Sep 27, 2009 11:09 am Post subject: RE:Python HELP |
|
|
Still not working for some of the values |
|
|
|
|
|
mms6
|
Posted: Sun Sep 27, 2009 12:18 pm Post subject: RE:Python HELP |
|
|
I got it, I divide time by afloat 60.0 and return the number of words in int()
My mistake for not reading carefully lol
Well, thanks to everyone who helped. |
|
|
|
|
|
|