Python Forum
My First Script - Looking For Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My First Script - Looking For Help
#1
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.
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020