Python Forum

Full Version: My First Script - Looking For Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Everybody,

This is my first script, and I am looking for advice...

I am trying to get the variable "response" to populate in another variable called body.

You can see here what I'm trying to do. I tried the following

def get_hostname():
    response = raw_input("Please enter your name: ")
	body = '[{"hostname": "response"}]'
...and...

def get_hostname():
    response = raw_input("Please enter your name: ")
	body = '[{"hostname": "print(response)"}]'
And I tried other variations but I can't seem to find the right way to do this.

Any help would be appreciated.
This section if for completed scripts.

However...
I presume that you want this:

def get_hostname():
    response = raw_input("Please enter your name: ")
    body = '[{"hostname": response}]' # without the quotes response will contain the input. Do you want body to be a string?
    return body
The print function returns None. Do not use it that way but just for output. Print is a function in Python 3. You are using raw_input which is deprecated in v3.x. Just input works the same way as raw_input in Python 3.
def get_hostname():
    response = raw_input("Please enter your name: ")
    body = '[{{"hostname": "{}"}}]'.format(response)
    return body

print get_hostname()
I expect you want something like this, but not sure about quotes, etc.
Also if you want to create a json, better use json module

import json
def get_hostname():
    response = raw_input("Please enter your name: ")
    body = [{'hostname':response}]
    return json.dumps(body)

print get_hostname()

Also, if you don't have really good reason for using python2 (like maintaining old code base) you should be using python3 (latest is python 3.6.5)