help with functions?

A forum for general discussion of the Python programming language.

help with functions?

Postby murster972 » Sun Sep 08, 2013 6:24 am

I'm on Codeacademy learning Python and can't understand what I'm doing wrong here?

the instructions were:
Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, we'd use s for string). The shut_down function should return "Shutting down..." when it gets "Yes", "yes", or "YES" as an argument, and "Shutdown aborted!" when it gets "No", "no", or "NO".

my code was:
Code: Select all
def shut_down(s):
    s.upper()
    if s == "yes" or "Yes" or "YES":
        return "Shutting down..."
    elif s == "no" or "No" or "NO":
        return "Shutdown aborted!"
    else:
        return "Sorry, I didnt't understand you."
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: help with functions?

Postby metulburr » Sun Sep 08, 2013 6:42 am

Code: Select all
if s == "yes" or "Yes" or "YES":

its s == 'yes' or s == 'Yes' or s == 'YES': etc.

However you can avoid all that just by reassinging s.lower() back to s, in which s would be a lowercase and you would not have to even check for anything uppercase

Code: Select all
s = s.lower()
if s == 'yes':
    ...


Yes and YES would be converted to just 'yes'
Code: Select all
>>> 'YES'.lower()
'yes'
>>> 'Yes'.lower()
'yes'
>>> 'yes'.lower()
'yes'
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: help with functions?

Postby murster972 » Sun Sep 08, 2013 6:45 am

Thank you, that worked :D
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests

cron