Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

my code returns twice for some reason

this is my code

''' import socket import time import random

server_name = 'bob'

def request_handler(request): if request == 'name': request_answer = server_name elif request == 'time': request_answer = time.ctime() return request_answer

print request_handler('name')

'''

and every time i run it i get the answer i asked for twice for example here i am requesting the server name with is bob and what i get when i run the script is the word 'bob' but twice and for the life of me i don't know why. thanks for the help in advance omri :)

p.s. am sure i'm missing something but i don't know what

4 Answers

I created a .py script file on my box with the following formatted code and it printed 'bob' in the terminal just one time. Is it possible you have a formatting error?

import socket 
import time 
import random

server_name = 'bob'

def request_handler(request): 
  if request == 'name': 
    request_answer = server_name 
  elif request == 'time': 
    request_answer = time.ctime() 

  return request_answer

print request_handler('name')

It looks as if you are printing the "request_handler('name')" at the bottom of your code, but in your request_handler definition it returns request_answer.

should i remove the return from my code then to make it stop returning twice ?

The method request_handler needs to return the parameter, otherwise your print statement won't print anything.

The print method is printing the value that gets returned from request_handler, which in this case, should be 'bob'

try typing "request_handler('name')" instead of "print request_handler('name')". The return statement should "print" it for you.

I'm curious to know what program you're using to run this? When I do this in a terminal window, nothing prints.

On second thought Jake, I think you my be correct about the formatting. I copied and pasted your code and I only got 'bob' once, not twice, and when I reformatted Omri's code I also got 'bob' once so I agree with you :]. I made a rookie mistake by not running the code before I answered.