For this code, I wanted to have a countdown when the code runs, however, the countdown wouldn't work ( For example, if I entered a time 22:20:00, and the present time is 23:10:00 then the countdown will start with 1:10:00.) I think the problem is in the for loop but I don't know how to change the loop so that the code would work.
I tried to add if else statement after each for loop because the for loop might not do the countdown for 0. Also, before trying this method I tried to make both the now time and set time into strings then subtract them, but there are also a problem to it.
import datetime
import pygame
import time as t
pygame.init ( )
hour = int ( input ( "Enter the the hour:" ) ) # alarm time
min = int ( input ( "Enter the minutes:" ) ) # alarm time
sec = int ( input ( "Enter the seconds:" ) ) # alarm time
song = str ( input ( "Enter the song that you want to set the alarm as: " ) ) # the name of the song
times = int ( input ( "How many times do you want it to play:" ) ) # the number of times the first song is going to play
more = str ( input ( "Do you want to add more songs after the one you already inputted:" ) )
totalsong = [ ]
now = datetime.datetime.now ( )
hourtotal=hour-now.hour
mintotal=abs(min-now.minute)
sectotal=abs(sec-now.second)
while more == "yes": # loop for whether or not to add songs
print ( "ok" )
song2 = list ( input ( "What song do you want to add:" ) )
totalsong = totalsong + song2 # putting it in the list so that the songs can continue playing
more = str ( input ( "Do you want to add more songs after the one you already inputted:" ) )
while True: # loop for playing the music and the countdown
if hour == now.hour and min == now.minute and sec == now.second: # check if the time now is the same as the set time
pygame.mixer.music.load ( song ) # play the first song
pygame.mixer.music.play ( times ) # the number of times
pygame.mixer.music.load ( str ( totalsong ) ) # play the list of songs
pygame.mixer.music.play ( 1 ) #number of times
break # stop the loop for countdown
else: # count down
for hours in range ( hourtotal , 0 , -1 ):
for minutes in range ( mintotal , 0 , -1 ):
for seconds in range ( sectotal , 0 , -1 ):
t.sleep ( 1 )
print ( hours , ":" , minutes , ":" , seconds ) # print out the countdown