Problem with "print" output for simple code

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

Problem with "print" output for simple code

Postby bumbler » Sun Oct 06, 2013 2:39 pm

I am trying to write a simple function that replicates what an experimenter would say each time the person be experimented on says that they want it to stop. The experiment will stop after the fifth time they ask to stop. When the print code at the end runs, it has an extra None between each of the output lines. The first None is okay, but I don't want the others to be there. Can anyone tell me why this is happening?

Here is the output that I receive:
IDLE 2.6.5 ==== No Subprocess ====
"Please continue."
"The experiment requires that you continue."
"It is absolutely essential that you continue."
"You have no other choice, you must go on."
"The experiment will end."

Here is the code that I have so far:
Code: Select all
# This function will take a number as a parameter, and return
# what the experimeter's reponse should be in the Milgram
# experiment when the subject asks to stop the experiment.
# The returned value should be words in quotes.

def subjectAsksToStop(numberOfTimesSubjectAskedToStop):
        if numberOfTimesSubjectAskedToStop == 1:
                print('"Please continue."')
        elif numberOfTimesSubjectAskedToStop == 2:
                print('"The experiment requires that you continue."')
        elif numberOfTimesSubjectAskedToStop == 3:
                print('"It is absolutely essential that you continue."')
        elif numberOfTimesSubjectAskedToStop == 4:
                print('"You have no other choice, you must go on."')
        elif numberOfTimesSubjectAskedToStop == 5:
                print('"The experiment will end."')           

# This code will run when you run this program.  It calls
# the subjectAsksToStop function with various parameters
# in order to test it.  It will print the result

print subjectAsksToStop(0)
print subjectAsksToStop(1)
print subjectAsksToStop(2)
print subjectAsksToStop(3)
print subjectAsksToStop(4)
print subjectAsksToStop(5)
Posts: 4
Joined: Fri Oct 04, 2013 12:42 am

Re: Problem with "print" output for simple code

Postby zeycus » Sun Oct 06, 2013 2:47 pm

When Python executs
Code: Select all
print subjectAsksToStop(0)

it invokes that function call, and prints the result. Inside your function subjectAskToStop, you have several prints. And then, the function ends without any 'return'. In these cases, the returned value is None. That is why it gets printed every time you call subjectAskToStop ends.

Live long and prosper.
User avatar
Posts: 23
Joined: Sun Feb 17, 2013 10:30 am
Location: Madrid

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests