Thread: I'm a Beginner
View Single Post
Old Dec 18th, 2007, 6:10 PM   #30
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,824
Rep Power: 5 Sane will become famous soon enough
Re: I'm a Beginner

Quote:
Originally Posted by titaniumdecoy View Post
Actually you can, although the result might not be what you would expect:

>>> print '%s' % ([1,2] * 2)
[1, 2, 1, 2]
>>> print '%s' % [1,2] * 2
[1, 2][1, 2]
Deters from my point. But yes...


@Chuckiferd:

As far as a "proper" implementation of this idea can go, it would take some familiarity with Python.

For a personal learning exercise, I'd say just go with if, print, and input statements. Those three things in conjunction could make your game.

If you want to actually do visible movement, you won't be able to do it this way...


A print statement outputs something to the user:
print "You Are At The Beginning Of A Yellow Brick Road"

The input function gets something from the user:
choice = input("""What do you want to do?
1) Walk In To The House
2) Pick Up A Penny Off The Ground
3) Twiddle Thumbs
Enter A Number: """)

That will output to the screen:
Quote:
What do you want to do?
1) Walk In To The House
2) Pick Up A Penny Off The Ground
3) Twiddle Thumbs
Enter A Number: <space for typing>
And prompt the user to type a number where I wrote <space for typing>. After this line executes, the variable "choice" will equal the number the user selected.

An if statement can branch your program in an alternate direction, according to the value of a variable. So if we wanted to do one thing if the user selected "1", and another if the user selected "2", it's as simple as:

if choice == 1:
    # The user selected 1. Do something here.
elif choice == 2:
    # The user selected 2. Do something different here.
else:
    # The user did not select 1 or 2. Make it default to option #3. And do something else!

These three things, "if", "print", and "input" could be combined to make your text adventure:

Python Syntax (Toggle Plain Text)
  1. print "You Are At The Beginning Of A Yellow Brick Road"
  2.  
  3. choice = input("""What do you want to do?
  4. 1) Walk In To The House
  5. 2) Pick Up A Penny Off The Ground
  6. 3) Twiddle Thumbs
  7. Enter A Number: """)
  8.  
  9. print
  10.  
  11. if choice == 1:
  12.  
  13. print "The house feels warm and welcoming, but a strange man (most likely the owner) is sitting on the couch looking very frightened of your presence."
  14. choice = input("""What do you want to do?
  15. 1) Quickly Apologize And Leave
  16. 2) Ask Him Who's House This Is
  17. 3) Enter Battle Mode!
  18. Enter A Number: """)
  19.  
  20. print
  21.  
  22. if choice == 1:
  23. print "etc"
  24. elif choice == 2:
  25. print "etc etc"
  26. else:
  27. print "etc etc etc"
  28.  
  29. elif choice == 2:
  30.  
  31. print "The penny was actually a piece of radioactive material. You sit there shocked and confused, and die 30 years later."
  32.  
  33. print "Game Over"
  34.  
  35. else:
  36.  
  37. print "While you're twiddling your thumbs, you notice there's a penny on the ground! But before you can pick it up an old man comes out of a house and snatches it from you, then heads back inside."
  38. choice = input("""What do you want to do?
  39. 1) Follow The Man Into His House
  40. 2) Continue Walking Down The Road
  41. 3) Continue To Twiddle Thumbs
  42. Enter A Number: """)
  43.  
  44. print
  45.  
  46. if choice == 1:
  47. print "etc"
  48. elif choice == 2:
  49. print "etc etc"
  50. else:
  51. print "etc etc etc"

Get it?

Here's an example of me running the programming, and playing the adventure:

Quote:
You Are At The Beginning Of A Yellow Brick Road
What do you want to do?
1) Walk In To The House
2) Pick Up A Penny Off The Ground
3) Twiddle Thumbs
Enter A Number: 1

The house feels warm and welcoming, but a strange man (most likely the owner) is sitting on the couch looking very frightened of your presence.
What do you want to do?
1) Quickly Apologize And Leave
2) Ask Him Who's House This Is
3) Enter Battle Mode!
Enter A Number: 3

etc etc etc
It will get messy really quick. But I think you'll learn something. You will need to do some really basic stuff to get comfortable with Python.
Sane is offline   Reply With Quote