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?

1

3 Answers 3

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.)

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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"

1 Comment

In Python 3, this is x = input() and "hello {}.".format(x).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.