This question already has answers here:
How do I convert seconds to hours, minutes and seconds?
(18 answers)
Closed 6 years ago.
I'm trying to breakdown a users integer input into hours, minutes, and seconds for a 24 hour period but having issues with going from pseudo code to actual code past the first hour equation. I want the final output to be in X hours, Y minutes, and Z seconds:
day = 86400
hour = 3600 #1 hour in a day * 60min * 60 sec
minute = 60
number = input("Choose a number between 0 and 86400: ")
while number != 0:
if number > 0:
newNumber = number / hour
number = newNumber
I'm teaching myself coding so I would love a simple approach to this problem... Am I even on the right track?
EDIT: I know there is a duplicate version of this question but that one simplifies things a bit too much (ironic) for me. I'm trying to learn by incremental steps but I do appreciate all the feedback
To convert a users input into X hours Y minute and Z seconds it depends on what they're inputting. If it's seconds, as it seems to be above, then do as the above says and take the seconds and divide it by 3600 for your hours, then subtract that number from the beginning number.
Also, it looks like you're using an infinite while loop, as you aren't prompting for input each time around.
If you want to do what you're trying to do I would suggest this:
number = input("Choose a number between 0 and 86400")
while(number != 0)
hours = number/3600
number = number-(hours*3600)
minutes = number/60
number = number-(minutes*60)
seconds = number
number = 0
print("The Time You Entered was " + str(hours) + " hours, " + str(minutes) + " minutes, and " + str(seconds) " seconds.")
Related
This question already has answers here:
How would I stop a while loop after n amount of time?
(11 answers)
Closed 9 months ago.
Good evening everyone. Guys I'm implementing a function that processes network packets. At the moment I'm using a while loop so that the function is always running, but I would like to set a time in ms and at the end of that time the loop ends. Can you give me a tip? A snippet of my loop so far:
while True:
status = pcap.loop(pd, 0, processing_pkts,
ct.cast(ct.pointer(packet_count), ct.POINTER(ct.c_ubyte)))
if status < 0:
break
Thanks for any tips!!!
import time
end_time = time.time() + 60 * 10 //this will run for 600 seconds
while time.time() < end_time:
// do your work
This question already has answers here:
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
Closed 6 months ago.
I am trying to write a function that asks the user how many days of sleep he wants to report, then according to the answer it should ask for the day of the week and the hours slept. The function must return a list of lists with the days of the week and the amount of hours the user reported sleeping.
So, if I call
print(healthy())
"How many days do you want to report?" : 3
Day of the week: Monday
Hours slept: 6
Day of the week: Tuesday
Hours slept: 8
Day of the week: Wednesday
Hours slept: 7
Then it should print, a list of list with the information of the user:
[['Monday', 6], ['Tuesday', 8], ['Wednesday', 7]]
So far I have:
def healthy():
record = []
days = int(input("How many days do you wanna report?: "))
DofW = input(str("which day?"))
Hr = int(input("how many hours?"))
for i in range(days):
record.append(DofW)
record.append(Hr)
print(record)
but this is not giving what I want. Pls help!
In your healthy(), you only asked "which day?" and "how many hours?" once, but according to your description, the asking should be put into the loop.
Besides, record is a list instead of a list of lists. What you want may be like this: record.append([DofW, Hr])
Lastly, if you want to call print(healthy()), then your healthy() is supposed to return something. Replace print(record) with return record, which is better practice anyway.
Ok, you need to actually return the record.
Also, you need to put the day in the for loop:
def healthy():
record=[]
days=int(input("How many days do you wanna report?: "))
for i in range(days):
DofW=input("\nwhich day? ")
Hr=int(input("\nhow many hours? "))
record.append([DofW,Hr])
return record
print(healthy())
def healthy():
record = []
for i in range(int(input('how many days do you want to report?'))):
record.append([input('which day?'), int(input('how many hours?'))])
print(record)
you need to loop through the amount of days, then each time the program asks for name of day and amount of hours, and pushes it into the record array
you are only asking for input once if you review your code. Secondly, you are appending item by item instead of appending a list.
def healthy():
record=[]
days=int(input("How many days do you wanna report?: "))
#i changed variable names to make more sense
for i in range(days):
#This method of iteration will constantly ask for input
day_input=input(str("Which day?"))
hours_input=int(input("how many hours?"))
#Append in a list of [day,hours]
record.append([day_input,hour_input])
print(record)
This question already has answers here:
Why does multiplication repeats the number several times? [closed]
(7 answers)
Closed 1 year ago.
I’ve written a piece of code that instead of print the product, prints a number a certain number of times. Whats wrong with it?
twelve = 12
name = input("What is your name? \nAnswer: ")
print("Cool name!")
nums = input("\n\nHow much pocket money did you receive last month?\nAnswer: ")
total = nums * twelve
print("\n\nI think you get ", total + " pounds in pocket money per year! Nice!")
The reason is that your nums variable is a string, which is the default with all Python inputs. Try converting it to int:
nums = int(input(...))
Or float if you are inputting a floating point number.
This question already has answers here:
How to round numbers
(2 answers)
Closed 5 years ago.
I like to get the run time of the script in rounded minutes.
I put this at the start
import time
start_time = time.time()
And this at the end
print ("My program took", time.time() - start_time, "to run")
so I get output value in seconds like 246.60637378692627 seconds
I like to get instead 4,11 minutes
Just divide your seconds by 60 and then use the round function:
time_taken = round((time.time() - start_time)/60.0, 2)
print ("My program took", time_taken, "to run")
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 6 years ago.
I am new to Python and am learning some basics. I would like to know why I am getting this error. The code is:
Hours = raw_input ("How many Hours you worked for today : ")
minutes = (Hours * 60)
Percentage = (minutes * 100) / 60
print "Today you worked : ", "percentage"
You have to convert your Hours variable to a number, since raw_input() gives you a string:
Hours = int(raw_input("How many hours you worked for today: "))
The reason why this is failing so late is because * is defined for string and int: it "multiplies" the string by the int argument. So if you type 7 at the prompt, you'll get:
Hours = '7'
minutes = '777777....77777' # 7 repeated 60 times
Percentage = '77777....77777' / 60 # 7 repeated 60*100 = 6000 times
So when it tries to do / on a string and a number it finally fails.
Hours is read as a string. First convert it to an integer:
Hours = int(raw_input("..."))
Note that Hours*60 works because that concatenates Hours with itself 60 times. But that certainly is not what you want so you have to convert to int at the first opportunity.
Your value Hours is a string. To convert to an integer,
Hours = int(raw_input("How many hours you worked for today : "))
Values in Python have a specific type, and although a string may contain only digits, you still can't treat it as a number without telling Python to convert it. This is unlike some other languages such as Javascript, Perl, and PHP, where the language automatically converts the type when needed.
raw_input() returns a string. Convert it to a number before proceeding (since multiplying a string by an integer is a valid operation).