Posts: 11
Threads: 4
Joined: Apr 2019
1. Define a function called compute_opening_duration to calculate the duration of the opening time.
The function takes two strings: the opening time and the closing time as the inputs and return the opening duration in hours.
The opening time and the closing time are in the form 'Hour:Minute:Second'.
If the opening time or the closing time are not in the correct form, the function returns -1.
Example:
opening time: '8:30:00', closing time: '16:00:00', return 7.5
opening time: '9:00:00', closing time: '16:15:00', return 7.25
opening time: '8:30:00', closing time: 'evening', return -1
Hint: you might want to use datetime package
import datetime
x= input('opening time: ')
y= input('closing time: ')
t = datetime.time(8:30:0)
z= datetime.time(16:0:0)
def compute_opening_duration(t, z):
if true
return 7.5
elif
if false
return 7.25
else Error: File "<ipython-input-8-08430a7c3799>", line 18
t = datetime.time(8:30:0)
^
SyntaxError: invalid syntax
thanks
Posts: 3,458
Threads: 101
Joined: Sep 2016
As the error states, number:number:number is not valid python syntax, except for slices. If you're unsure how something works, check the docs: >>> import datetime
>>> help(datetime.time)
Help on class time in module datetime: Or the docs: https://docs.python.org/3/library/dateti...etime.time
Posts: 11
Threads: 4
Joined: Apr 2019
ok I've made the changes. I thinks its looking better but its obviously still wrong
import datetime
x= input('opening time: ')
y= input('closing time: ')
o = datetime.time(8,3,0)
p= datetime.time(16,0,0)
def compute_opening_duration(t, z):
if t==8,3,0 and z==16,0,0
return 7.5
elif
if 8,3,1<t<8,29,59 or 16,0,1<z<15,59,59
return 7.25
else
return -1 Error: File "<ipython-input-10-b0f49fce2ac6>", line 21
if t==8,3,0 and z==16,0,0
^
SyntaxError: invalid syntax
Posts: 4,220
Threads: 97
Joined: Sep 2016
Note that you can subtract times from each other. That gives you a timedelta object, expressing how far apart those times are.
Posts: 11
Threads: 4
Joined: Apr 2019
I'm not really looking at time duration. I'm just trying to return 7.5 if it opens at 8:30 and claoses at 4, 7.25 if it is any other time and -1 if the user enters something in the correct format.
What should I try now?
Posts: 3,458
Threads: 101
Joined: Sep 2016
Apr-06-2019, 05:20 PM
(This post was last modified: Apr-08-2019, 12:42 AM by nilamo.)
First, fix the syntax errors. Once the program actually runs, that's when you can start thinking about whether or not it's doing the right thing.
You've got a syntax error saying that this: if t==8,3,0 doesn't make sense. So, when you type that, what are you trying to accomplish?
Posts: 11
Threads: 4
Joined: Apr 2019
I guess I want it so someone types 8,3,0 into the input and it returns 7.5.
I've only been doing python for 3 weeks so I can't understand wy its not working
Posts: 1
Threads: 0
Joined: Apr 2019
It's not working , I don't know either why.
Posts: 13
Threads: 0
Joined: Mar 2019
What exactly is "t" and "z"? I understand that they are parameters for your method "compute_opening_duration", but here's the problem:
1. As others have mentioned, the syntax.
2. t and z aren't utilized at all, initialized, nothing. You did initialize o, and p to the values you're checking t and z to be though, so that could be simplified to if t == o and z == p
3. You say you want to print a specific thing if the value is set to a specific value, why not just check for that? Your teacher doesn't want you simply returning 7.5 or 7.25, your teacher probably wants you to do some math in order to realize what should be returned
Please fix your code and come back with some kind of snippet, and try to explain a little more what you're looking for exactly as the assignment says. Just copy/paste the assignment don't paraphrase it because then it's harder to help.
Posts: 11
Threads: 4
Joined: Apr 2019
(Apr-08-2019, 02:17 PM)Emiriya Wrote: It's not working, I don't know either why. 
All good. Thanks for trying. I will contact my teacher and also ask on other forums. Hopefully someone will know :)
|