coding neophyte requesting assistance

This is the place for queries that don't fit in any of the other categories.

coding neophyte requesting assistance

Postby fizix » Thu Jan 23, 2014 9:59 pm

I just made the following code on codecademy...

Code: Select all
cont = True

def shut_down(s):
    if s.lower()=='yes':
        return "Shutting down..."
    elif s.lower()=='no':
        return "Shutdown aborted!"
    else:
        cont = True
#        print cont
        return "Sorry, I didn't understand you."

while cont:
    res=raw_input('Shutdown queued.  Shall I?')
    cont = False
    print shut_down(res)
#    print cont

print 'loop exit'


I'm using 'blargh' as my raw input string.

Currently, the print in the else part of the function prints True, but the print in the while loop prints False. And then I see 'loop exit' when I want my code to ask for the variable res again.

Why???

Thanks for the help~
Last edited by micseydel on Thu Jan 23, 2014 10:03 pm, edited 1 time in total.
Reason: Locked OP, code tags.
fizix
 
Posts: 2
Joined: Thu Jan 23, 2014 9:53 pm

Re: coding neophyte requesting assistance

Postby ochichinyezaboombwa » Thu Jan 23, 2014 10:15 pm

There are a few problems; most importantly you don't use a result of your function (although you do return something).
A function like this should better return a boolean result; to shut down or not? Here is one way to do it:
Code: Select all
def shut_down():
    while True:
        s=raw_input('Do you wnat to shut down?')
        if s.lower()=='yes':
            return True
        elif s.lower()=='no':
            return False
        else:
            print "What do you mean? Please only answer 'yes' or 'no'"

while True:
    # do some interesting stuff...
    if shut_down():
        break

ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: coding neophyte requesting assistance

Postby fizix » Thu Jan 23, 2014 10:24 pm

Thanks for the reply, ochi~ That gave me some insight into how code is actually done.

If anyone isn't familiar with codecademy, it's a site that teaches newcomers how to code. I'm learning Python, and went an extra step on the assignment by using a while loop, which the lessons haven't taught me yet.

Though helpful, your response hasn't answered my key question (though I suppose the original post could've phrased it clearer).

My code should've assigned the variable cont to be True in the final else. My (commented out) print line confirmed that. However, after I print shut_down(res), the (commented out) print line prints False instead. Then the loop exits and I see 'loop exit'.

I don't understand why the code doesn't stay in the while loop. Why is my variable changing to False when I don't want it to? 'blargh' as raw_input should keep me in the loop, and I don't understand why it won't.

EDIT: This may be because I don't actually have Python installed on my machine. If there's nothing crucially wrong with my code, could someone try copy-and-pasting it into their installed Python to see if it works?
fizix
 
Posts: 2
Joined: Thu Jan 23, 2014 9:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 4 guests