Cooldown format in days discord.py

2024/7/7 7:54:40

I'm currently making a command which has a cooldown of 5 days, and I'm currently using this code for the cooldown.

def better_time(self, cd:int):time = f"{cd}s"if cd > 60:minutes = cd - (cd % 60)seconds = cd - minutesminutes = int(minutes/ 60)time = f"{minutes} minutes {seconds} seconds"if minutes > 60:hoursglad = minutes -(minutes % 60)hours = int(hoursglad/ 60)minutes = minutes - (hours*60)time = f"{hours} hours {minutes} minutes {seconds} seconds"return time

How would I do days? I have tried the following (it has confused me):

def better_time(self, cd:int):time = f"{cd} seconds"if cd > 60:minutes = cd - (cd % 60)seconds = cd - minutesminutes = int(minutes/ 60)time = f"{minutes} minutes {seconds} second(s)"if minutes > 60:hoursglad = minutes -(minutes % 60)hours = int(hoursglad/ 60)minutes = minutes - (hours*60)time = f"{hours} hours {minutes} minute(s) {seconds} second(s)"if hours > 86400:daysglad = minutes -(hours % 60)days = int(daysglad/ 60)minutes = minutes - (days*60)time = f"{days} days {minutes} minute(s) {seconds} second(s)"return time```Yes, I know it's wrong, it has confused me.
Answer

You should starts at days, later hours, later minutes and seconds

cd = 500_000days = cd // 86400    # 24*60*60 seconds
rest = cd  % 86400hours = rest // 3600  #    60*60 seconds
rest  = rest  % 3600minutes = rest // 60  #       60 seconds
seconds = rest  % 60print(f"{cd} seconds = {days} day(s) {hours} hour(s) {minutes} minute(s) {seconds} second(s)")

Result:

500000 seconds = 5 day(s) 18 hour(s) 53 minute(s) 20 seconds(s)

And if you want to display shorter version when there is no days/hours/minutes/seconds then you could use if/else after caculating all values

result = [f"{cd} seconds ="]if days:result.append(f"{days} day(s)")if hours:result.append(f"{hours} hour(s)")if minutes:result.append(f"{minutes} minute(s)")if seconds:result.append(f"{seconds} seconds(s)")print(" ".join(result))
https://en.xdnf.cn/q/120141.html

Related Q&A

From scraper_user.items import UserItem ImportError: No module named scraper_user.items

I am following this guide for scraping data from instagram: http://www.spataru.at/scraping-instagram-scrapy/ but I get this error:mona@pascal:~/computer_vision/instagram/instagram$ ls instagram scrap…

Correct way of coding a Guess the Number game in Python [closed]

Closed. This question is off-topic. It is not currently accepting answers.Want to improve this question? Update the question so its on-topic for Stack Overflow.Closed 11 years ago.Improve this questio…

How do i stop the infinite loop for the if and elif part?

c = random.randint(0,5) guess =int(input("=")) while True:if guess > c:print("you failed the test")elif guess <c: print("you messed up")else:print("you are the …

How to zip a list of lists

I have a list of listssample = [[A,T,N,N],[T, C, C, C]],[[A,T,T,N],[T, T, C, C]].I am trying to zip the file such that only A/T/G/C are in lists and the output needs to be a list[[AT,TCCC],[ATT,TTCC]]W…

I want to make a Guess the number code without input

import random number = random.randint(1, 10)player_name = "doo" number_of_guesses = 0 print(I\m glad to meet you! {} \nLet\s play a game with you, I will think a number between 1 and 10 then …

How to zip keys within a list of dicts

I have this object: dvalues = [{column: Environment, parse_type: iter, values: [AirportEnclosed, Bus, MotorwayServiceStation]}, {column: Frame Type, parse_type: list, values: [All]}]I want a zipped out…

AttributeError: DataFrame object has no attribute allah1__27

Im trying to solve this and Im pretty sure the code is right but it keeps getting me the same Error.I have tried this:import datetime from datetime import datetime as datettest_df = shapefile.copy() te…

How to convert csv to dictionary of dictionaries in python?

I have a CSV file shown below.I need to convert CSV to dictionary of dictionaries using python.userId movieId rating 1 16 4 1 24 1.5 2 32 4 2 47 4 2 …

Mo Money- Making an algorithm to solve two variable algebra problems

A cash drawer contains 160 bills, all 10s and 50s. The total value ofthe 10s and 50s is $1,760.How many of each type of bill are in the drawer? You can figure thisout by trial and error (or by doing a…

How to explode Python Pandas Dataframe and merge strings from other dataframe?

Dataframe1 has a lot of rows and columns of data. One column is Text. Certain rows in Text column have strings and some strings include within the strings this {ExplodeEList2} How to explode (expand) t…