-1

My code is:

import time

local_time = time.localtime()
time_string_d = time.strftime("%d", local_time)
time_string_m = time.strftime("%m", local_time)
time_string_y = time.strftime("%Y", local_time)
time_string_hm = time.strftime("%H:%M", local_time)
day_st = ['1', '21', '31']
day_nd = ['2', '22', '32']
day_rd = ['3', '23']
day_th = ['4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', 
'19', '20', '24', '25', '26', '27', '28', '29', '30']

def month():
    if time_string_m == '1':
        print('It is ' + time_string_hm)
        print('January ', day())
    if time_string_m == '2':
        print('It is ' + time_string_hm)
        print('February ', day())
    if time_string_m == '3':
        print('It is ' + time_string_hm)
        print('March ', day())
    if time_string_m == '4':
        print('It is ' + time_string_hm)
        print('April ', day())
    if time_string_m == '5':
        print('It is ' + time_string_hm)
        print('May ', day())
    if time_string_m == '6':
        print('It is ' + time_string_hm)
        print('June ', day())
    if time_string_m == '7':
        print('It is ' + time_string_hm)
        print('July ', day())
    if time_string_m == '8':
        print('It is ' + time_string_hm)
        print('August ', day())
    if time_string_m == '9':
        print('It is ' + time_string_hm)
        print('September ', day())
    if time_string_m == '10':
        print('It is ' + time_string_hm)
        print('October ', day())
    if time_string_m == '11':
        print('It is ' + time_string_hm)
        print('November ', day())
    if time_string_m == '12':
        print('It is ' + time_string_hm)
        print('December ', day())
    else:
        print('There was an error in the month detecting system.')

def day():
    if time_string_d in day_st:
        print('st')
    if time_string_d in day_nd:
        print('nd')
    if time_string_d in day_rd:
        print('rd')
    if time_string_d in day_th:
        print('th')
    else:
        print('There was an error in the day detecting system.')

and I want it to print the following:

It is (current time for example: 14:20)
(current month for example: October, current day + /st/nd/rd/th for example 8th)

Like this:

It is 14:20
October 8th

Also if it fails then the following:

There was an error in the month detecting system

or

There was an error in the day detecting system.

What it actually prints out:

It is 14:20
There was an error in the day detecting system.
October  None
There was an error in the month detecting system.
Ákos Molnár
  • 75
  • 1
  • 6
  • Putting multiple `if` statements after each other does not do what you seem to think. – mkrieger1 Oct 08 '21 at 12:27
  • 1
    You already use `strftime`, so why not use it to build the entire string? `time.strftime("%B %#d");` → "October 8". (On non-Windows systems it's `time.strftime("%B %-d");`) – 001 Oct 08 '21 at 12:33
  • 2
    The only thing that needs special handling is "st, nd, rd, th", for that see https://stackoverflow.com/q/5891555/476. The rest can be done purely with the right `strftime` formatting string. Properly you'd use a localisation library like babel, which includes the "st, nd, rd, th" handling too… – deceze Oct 08 '21 at 12:33
  • I agree with the note left by deceze, that link's method should make things much easier to handle than chaining if-statements – Zaid Al Shattle Oct 08 '21 at 12:36

2 Answers2

1

You should use single if-elif-else for example

x = 1
if x == 1:
    print(1)
elif x == 2:
    print(2)
else:
    print("else")

will print 1, whilst

x = 1
if x == 1:
    print(1)
if x == 2:
    print(2)
else:
    print("else")

will print 1 then else because it has 2 if statements - if and if-else. You might use 0 or more elifs in single if statement, if you want to know more read linked docs.

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

The issue here is that you are chaining if statements instead of elifs, when your code is running, it checks each line, but the else statement runs in every case where:

if time_string_d in day_th:

is not true. As in, your function looks like this:

def day():

    if time_string_d in day_st:
        print('st')

    if time_string_d in day_nd:
        print('nd')

    if time_string_d in day_rd:
        print('rd')

    if time_string_d in day_th:
        print('th')
    else:
        print('There was an error in the day detecting system.')

and only this part matters for the else statement:

if time_string_d in day_th:
    print('th')
else:
    print('There was an error in the day detecting system.')
Zaid Al Shattle
  • 1,454
  • 1
  • 12
  • 21