Help with Code for School Project

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

Help with Code for School Project

Postby bumbler » Fri Oct 04, 2013 12:59 am

I just started a computer course that involves basic Python programming. Unfortunately, I'm not finding it so 'basic'. The first couple of tasks were easier, but I struggling with the next part.

We were given this code as starting point and have to finish it according to the requirements below:

def askUserToSayHello():
return raw_input('Please say hello: ')


I need to finish the code above so that it satisfies the following requirements:
1. There is a line of code before the loop that sets done to False. Where done represents whether the user has done what was asked of them - that is, they typed hello into the shell when asked

2. There is a while loop that will continue to loop until done is True

3. The function askUserToSayHello is used in the loop to repeatedly ask the user to say hello until they actually do.

4. When the user says hello, the program should print ‘Thanks!’ before finishing. Note that the result of askUserToSayHello can be compared against the equality
operator, which is written as ==

5. When the user says anything else, the program should print some sort of warning
Last edited by micseydel on Fri Oct 04, 2013 4:59 am, edited 1 time in total.
Reason: First post lock.
bumbler
 
Posts: 4
Joined: Fri Oct 04, 2013 12:42 am

Re: Help with Code for School Project

Postby hansn » Fri Oct 04, 2013 8:52 am

That's pretty basic stuff. You should at least show us some effort you've made.

If you're completely stuck I suggest you review some of the material you probably got from your course.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Help with Code for School Project

Postby bumbler » Fri Oct 04, 2013 12:10 pm

Sorry....should've shared what I had so far.

Code: Select all
##
# This function asks the user to say hello, and returns what the user
# input.

def askUserToSayHello():
    return raw_input('Please say hello: ')

##
# This is where you will create a while loop that continuously
# asks the user to say hello until they actually do

done = False

while done == False:
    greeting=raw_input('Please say hello: ')
    if greeting != 'hello':
        print('You made a mistake. You must enter "hello" as shown.')
    elif greeting == 'hello':
        print('Thanks!')
        done = True


So, it works for me as is. I'm not sure if the 'done' statements are doing anything but the program stops where it is supposed when run. But, the code that I wrote doesn't use the askUserToSayHello function and I am not sure how to use it.
bumbler
 
Posts: 4
Joined: Fri Oct 04, 2013 12:42 am

Re: Help with Code for School Project

Postby Kebap » Fri Oct 04, 2013 12:39 pm

bumbler wrote:the code that I wrote doesn't use the askUserToSayHello function and I am not sure how to use it.

Here is how you do it:

Code: Select all
def askUserToSayHello():
    return raw_input('Please say hello: ')

while done == False:
    greeting = raw_input('Please say hello: ')

change this last line so:
Code: Select all
def askUserToSayHello():
    return raw_input('Please say hello: ')

while done == False:
    greeting = askUserToSayHello()

Simple. :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Help with Code for School Project

Postby bumbler » Fri Oct 04, 2013 2:03 pm

Thanks. Make sense...by inserting there, it just tells the program go find that function.
bumbler
 
Posts: 4
Joined: Fri Oct 04, 2013 12:42 am


Return to General Coding Help

Who is online

Users browsing this forum: Larz60+ and 5 guests