1

I am making a simple text based game but I can't even get past the second line of code. My code so far looks like this:

print("Welcome To City Text!  First You Must Name Your City.")

Then the person is supposed to type a name and the shell will print "Welcome To "Town Name" City" The problem is I don't know how to do this. Does anyone know how to do this?

TerryA
  • 58,805
  • 11
  • 114
  • 143
Ethan Bacon
  • 233
  • 1
  • 2
  • 7

3 Answers3

5

From the fact that you're using print("text") rather than print "text", I assume you're using Python 3.x rather than Python 2.x. In that case, raw_input won't work, because that was renamed input (the original input function did something else, and was removed entirely).

So, if you're getting a NameError when you use raw_input, just replace it with input.

(If you aren't getting a NameError, you're using Python 2.x, and you should leave out the parenthesis around the string you're printing; in Python 2.x, print is a statement, not a function. It will still work with the parentheses, but it's just going to create confusion.)

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
1

The line raw_input() lets you get the input from the console. The argument you pass it is the line it prints before getting the input. So your code would look something like this:

var_name=raw_input("Welcome To City Text! First You Must Name Your City.")

this will print: Welcome To City Text! First You Must Name Your City. then let you type until you hit enter and return what you typed.

Chachmu
  • 7,725
  • 6
  • 30
  • 35
0

You can use x=raw_input() to get a string from the keyboard.

If you google "python input text from console" you will get lots of answers.

You can put the result into another string using

print("hello %s." % x)

You can also use

print("hello "+x+".")

which does more or less the same thing.

You can find more about this by googling for "formatting strings in python" or "concatenating strings in python"

Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58