-1

So I have a simple python program that runs a command that starts the php built in server. It has always worked, but I decided I wanted to add a piece of code that would let me choose between hosting locally and publicly. This piece of code is inside the arrows (/\ \/):

#Import the os package so that this code can run commands
import os

#Get the port that the user wants to host on
port = str(input("What port would you like to host on? "))

#\/\/\/\/\/\/\/\/\/\/\/
#Decide whether or not to make public or private
choice = ''
tupleL = ('l', 'L')
tupleP = ('p', 'P')
while True:
    choice = str(input("Type 'L' to host locally or 'P' to host publicly: "))

    if choice in tupleL:
        host = "localhost"
        break
    elif choice in tupleP:
        host = str(input("What is your global IP address? "))
        break
    else:
        print "That wasn't an option!"
#/\/\/\/\/\/\/\/\/\/\/\

#Add wanted port and host to the command that hosts the php server
cmd = "php -S " + host + ":" + port

#Actually run the command to host the php server
os.system(cmd)

Everything works fine up until choice = str(input("Type 'L' to host locally or 'P' to host publicly: ")). Whenever I put anything in, no matter what it is, I get this:

What port would you like to host on? 8080
Type 'L' to host locally or 'P' to host publicly: a
Traceback (most recent call last):
  File "host_php.py", line 13, in <module>
    choice = str(input("Type 'L' to host locally or 'P' to host publicly: "))
  File "<string>", line 1, in <module>
NameError: name 'a' is not defined

I originally had problems with input() taking string as an object, but I easily solved that by using str(input()). The only possible reason that I could think of was that I hadn't defined the variable choice, but that was not the reason as putting choice = '' did not fix this.

I have no idea how I am supposed to fix this, and I haven't found a solution anywhere. Any help will be greatly appreciated.

EDIT: In response to the duplicate mark, this question is different due to our differing circumstance, but I can see that the other questioned mentioned does answer my question.

Andbeav
  • 22
  • 7

1 Answers1

1

Looks like you're running Python 2, so you should use raw_input instead of input.

input in python 2 tries to evaluate the input string as Python code in the local scope, which is not what you want, according to your code.

Mikhail Burshteyn
  • 4,762
  • 14
  • 27