'Input' help

This is the place for queries that don't fit in any of the other categories.

'Input' help

Postby murster972 » Thu Aug 22, 2013 8:33 pm

I have just started making a text based adventure game. I have used an input function, for the players movement, but
when ever i press enter after entering an option and pressing enter the window just closes???

heres the code.

Code: Select all
print "This is a text based adventure game called --Iron Fud--"
print "The goal of the game is to find and defeat the King!"
print "Play through and Enjoy the Game! :)"
print ""
print "You are In a small dark room. There is a lamp on the ground"
print "and a door in front of you."
print ""
input("Option (n, s, e, w): ")

if input == "pick up lamp":
   print "You now have a lamp"

elif input == "n" or "e" or "w" or "s":
   print "You cannot go there"
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: 'Input' help

Postby micseydel » Thu Aug 22, 2013 9:15 pm

You want raw_input() rather than input(). input() tries to interpret the user's input as a Python expression (usually a number, but it could be any arbitrary Python code including stuff that wipes your hard drive). The generally recommended way to get a number from raw_input() is to call int() or float() on the string that raw_input() returns.

Also, the variable "input" (or "raw_input") doesn't change after you call it as a function, you need to take the return value and do something with it. For example,
Code: Select all
name = raw_input("What's your name? ")
print "Hi, {}!".format(name)

Just doing
Code: Select all
raw_input("What's your name? ")
print "Hi, {}!".format(raw_input)

Will not do what you want.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1179
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: 'Input' help

Postby murster972 » Fri Aug 23, 2013 6:06 am

Thank you :D
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: 'Input' help

Postby murster972 » Fri Aug 23, 2013 6:08 am

Im stilling having the same problem, that every time i press enter after selecting an option, the window closes??? :(
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: 'Input' help

Postby Mekire » Fri Aug 23, 2013 6:22 am

Put another prompt at the very end of your program that says:
Code: Select all
raw_input("Thanks for playing. Press enter to exit.")

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: 'Input' help

Postby murster972 » Fri Aug 23, 2013 9:11 am

Okay i'll try that, thank you for the help
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests