0

The problem with the following code is on line 6 is that it will return false if the user inputs a day of the week with a capital. i.e "Tuesday". I am wanting this to return as "(day.capitalize())"

day = input("Please enter a day of the week")

def verify_day(day):
    days_list = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
    for x in days_list:
        if x == day or x.capitalize == day:
            return (day.capitalize())
        elif day not in days_list:
            return ("false")


print(verify_day(day))
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Droid2
  • 65
  • 1
  • 7
  • 2
    Possible duplicate of [How do I do a case insensitive string comparison in Python?](http://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison-in-python) – Chris_Rands May 03 '17 at 09:38
  • Also see http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values – Chris_Rands May 03 '17 at 09:38

6 Answers6

3
if x == day.lower()

That way you don't have to check two instances. You can also just say

if day.lower() in days_list

and save the loop

For more clarity:

def verify_day(day):
    day_list = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
    if day.lower() in day_list:
        return day.upper()
    else:
        return False
TLOwater
  • 638
  • 3
  • 13
  • Your verify day function would now be days_list and then if day.lower() in days_list return day else return False – TLOwater May 03 '17 at 09:43
  • so will this print out the day even if you type in something with a capital? i.e "Tuesday" as before this was printing out false. – Droid2 May 03 '17 at 09:48
  • Yes, the .lower() function turns it all lowercase letters. This is checked within the list of valid days and if it is Tuesday for example it will evaluate to true. If true is triggered it will return the day capitalised (as in your original but using the .upper() function). If not it will return False – TLOwater May 03 '17 at 09:50
  • It works! I'm guessing the for loop was unnecessary or this way was just easier. – Droid2 May 03 '17 at 09:54
  • Glad to hear it, the `for` loop is often the right way to compare values from different data objects but for this example the `in` option is a little faster and neater. – TLOwater May 03 '17 at 09:55
3

A more obfuscated solution can be using the trick that and returns the last evaluated element if all elements are true, otherwise it will stop evaluating when one of the conditions is false.

Example:

13 and 23 returns 23 instead of True

False and 33 returns False (it doesn't even evaluate the second element)

So you can use this trick and do:

return day.lower() in days_list and day.capitalize()

This will first evaluate if day.lower() in days_list, if your day is not in the list will short circuit the and and return False.

Otherwise it will return the second evaluated condition which is day.capitalize()

  • Considering OPs python experience I don't think this is necessarily the best option (because of readability) but that's a neat trick. – TLOwater May 03 '17 at 09:53
2

You could remove all capital letters before the comparison :

import string
day = input("Please enter a day of the week")

def verify_day(day):
    days_list =["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
    for x in days_list:

        if x == day.lowercase: # <----- this would accepte any capitalization of the input

            return (day.capitalize())
        elif day not in days_list:
        return ("false")

You don't have to iterate on the list by the way, you could just check :

    def verify_day(day):
        days_list =["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
        if day.lowercase() in days_list :
            return (day)
        else :
            return ("false")
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
pwnsauce
  • 416
  • 4
  • 14
0

You can ignore case from comparison.

Refer this question.

day = day.rstrip().lower()
if x == day
  return (day.capitalize())
elif day not in days_list:
  return ("false")
Community
  • 1
  • 1
ThisaruG
  • 3,222
  • 7
  • 38
  • 60
  • My problem still remains if I enter "Tuesday" or something with a capital it will print out "false" when I am wanting to print out "Tuesday". – Droid2 May 03 '17 at 09:45
  • @Droid2 Applying lower before comparison may resolve the problem. – ThisaruG May 03 '17 at 09:53
0

You should first lower the day input for the purpose of the check and then capitalize it for the return value.

day = input("Please enter a day of the week")
def verify_day(day):
  days_list = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
  if day.lower() in days_list:
      return day.capitalize()
  else:
      return False
Liran Funaro
  • 2,750
  • 2
  • 22
  • 33
0

always compare day.lower() with your data_list item and return day as it is received by doing this user will get whatever day value he/she entered (i.e. Monday or monday).

day = input("Please enter a day of the week")

def verify_day(day):

days_list = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

for x in days_list:
    if x == day.lower():
        return (day)
    elif day not in days_list:
        return ("false")


print(verify_day(day))
DexJ
  • 1,264
  • 13
  • 24
  • and one suggestion use "in" with list this will help to omit loop. you can directly check i if condition like ---> if day.lower() in day_list: ... – DexJ May 03 '17 at 09:48