-1
from datetime import date
import calendar

my_date = date.today()
today_day = calendar.day_name[my_date.weekday()]

if today_day == 'Wednesday' or 'Thursday':
    place = 'A'
else:
    place = 'B'

Initially, I wanna set place as 'A' when 'today' is 'Wednesday' or 'Thursday', otherwise as 'B'. But the designed code kept returning 'A' despite it is 'Saturday' today(14/10/2017). How could I fix it?

yeungcase
  • 383
  • 2
  • 3
  • 12

4 Answers4

2

Change your condition to:

if today_day == 'Wednesday' or  today_day == 'Thursday':

or:

if today_day in ('Wednesday', 'Thursday'):

In your version 'Thursday' is a non-empty string and as such always truthy when evaluated as an atomic part of a boolean expression.

user2390182
  • 72,016
  • 6
  • 67
  • 89
2

Your if statements is eqivalent to

if (today_day == 'Wednesday') or 'Thursday':

and False or 'none empty string' will always evaluate as True.

I would implement this as

weekday = date.today().weekday()
place = 'A' if weekday in [3,4] else 'B'
Udo Klein
  • 6,784
  • 1
  • 36
  • 61
2

What you have written is equivalent to boolean or of two conditions

(today_day == 'Wednesday') or 'Thursday'

Thursday always evaluates to True (because that's how non-empty strings are evaluated in python).

Instead, you could write the (more idiomatic)

today_day in ('Wednesday' or 'Thursday')

or

today == 'Wednesday' or today == 'Thursday'
blue_note
  • 27,712
  • 9
  • 72
  • 90
0

The issue is you are asking if (today_day == 'Wednesday') or 'Thursday'. 'Thursday' evaluates to True since it is not an empty string, so place will always be A. What you need to do is

if today_day == 'Wednesday' or today_day == 'Thursday':
    place = 'A'
else:
    place = 'B'
jpyams
  • 4,030
  • 9
  • 41
  • 66