I am a novice programmer in python, and I have this one exercise that seems to stump me and a lot of others, I would really appreciate some help!
This is the problem: Write a program that asks the user to enter a number of seconds, and works as follows:
There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds.
There are 3600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to 3600, the program should display the number of hours in that many seconds.
There are 86400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86400, the program should display the number of days in that many seconds.
The code I have so far:
print('enter a number of seconds')
seconds = int(input('enter a number of seconds'))
if seconds >=60 [seconds] / 60:
if seconds >=3600 [seconds] / 3600:
if seconds >=86400 [seconds] / 86400
The problem we get when we run this is:
Traceback (most recent call last):
File "main.py", line 5, in
if seconds >=60 [seconds] / 60:
TypeError: 'int' object is not subscriptable
What does this mean?
3 Answers 3
1) Your program isn't printing the number you are calculating because you aren't asking it to print anything.
(and you aren't calculating anything)
2) Your don't have remotely valid python syntax.
What the heck is
if seconds >=60 [seconds] / 60:
Can you read that out loud to me?
The error message that I think you are getting (it's the one that I get when I run your code, so I put it in your question) is saying:
TypeError: 'int' object is not subscriptable
It's saying that because thing [other thing] syntax is a subscripting operation.
You are doing 60[seconds]. This says "take the seconds element from the 60 array". Python can't understand that. 60 is an integer, not an array. Integers are not subscriptable. So that's what it told you.
You want something like:
if seconds >= 60: # if seconds is greater than 60
if seconds >= 3600: # and it's greater than 3600
if seconds >= 86400: # and it's greather than 86400
print seconds/86400 # then it's really big so divide by the big number and print
else:
# here, it's less than 86400 and more than 3600
print seconds/3600 # so divide by 3600
else:
# here it's a minute kind of number
print seconds/60
else:
# its less than 60
print seconds
Note that this is by far not the most elegant way to do it, it's just some logic similar to yours, but with approximately valid python syntax.
Note that this is python 2.x syntax. If you are using python 3.x, add that as a tag to your question.
Comments
Welcome to the world of programming!
I've created an example which is close to what you need. You should be able to work from it to get your answer. Some of what's in here might be a little advanced for you at the moment, but if you follow the link below, you can play with it and learn from it.
Link to run the code: http://repl.it/1d0
#When you put something after a '#' symbol, it's a comment!
#This lets you explain your code to other people.
#Rather than typing these numbers over and over,
#you should store them in variables so that you can reuse them.
secondsPerMinute = 60
secondsPerHour = 60*secondsPerMinute
secondsPerDay = secondsPerHour*24
#This is a function. Like a function in math, it takes in several values as 'parameters',
#and then returns a value back. This function takes a number and returns its rounded value.
#See if you can figure out how it works.
def round(number):
return int(number + .5)
#This function takes a number and a unit, and uses them to make a sentence.
def say(number, unit):
print "That's {0} {1}s!".format(number, unit)
print "That's {0} {1}s if you round to the nearest number!".format(round(number), unit)
print('Enter a number of seconds:')
seconds = float(input())
#In this series of if-statements, we'll go through and figure out
#the most appropriate unit of time to use, and then store two values
#to use with the say function.
if seconds >= secondsPerDay:
number = seconds / secondsPerDay
unit = "day"
elif seconds >= secondsPerHour:
number = seconds / secondsPerHour
unit = "hour"
elif seconds >= secondsPerMinute:
number = seconds/secondsPerMinute
unit = "minute"
else:
number = seconds
unit = "second"
#We're calling the say function using the variables from above as 'arguments'.
say(number, unit)
Comments
To add to GreenAsJade's answer, you can write the conditions like this to avoid the unnecessary nesting.
if seconds >= 86400:
print seconds/86400, "day(s)"
elif seconds >= 3600:
print seconds/3600, "hour(s)"
elif seconds >= 60:
print seconds/60, "minute(s)"
if seconds >=60 [seconds] / 60: