Chelsea
Chelsea

Reputation: 23

How to print variable on multiple lines and call on a single piece of code many times in Python?

1.I'm using break to break out of a loop, but I don't know how to make the program keep going no matter what unless this happens. Just typing in while: is invalid (or so the progam tells me) and I want the game to keep going even if the user types in an emptry string.

2.Is there a way to not have to re-type a bit of code every time I need it? I have a bunch of responses for the program to spit out that I'll have to use many times:

if action[0]=='go':
    print("You're supposed to go to David!")
elif action[0]=='look':
    print("You can't see that")
elif action[0]=='take':
    print("You don't see the point in taking that.")
else:
    print("I don't recognise that command")

Where action is a list from the player's input. Or do I just have to type it out again each time?

I don't know how to define a function that does the above, and I'm not even sure that's what I'm supposed to do.

3.Some story descriptions I'm using are a very long stings and I don’t want players to have to scroll sideways too much. But I want to just define them as variables to save myself some typing. Is there a way around this. Or do I just have to type it out every time with print(“““a string here”””)

4.If the string starts with 'look' and has 'floor' or 'mess' or 'rubbish' in it, I want it to print a certain output. This is what I currently have:

if action[0]=='look':
    if 'floor' in action or 'rubbish' in action or 'trash' or 'mess' in action:
        print('onec')
    elif 'screen' in action or 'computer' in action or 'monitor' in action:
        print('oned')
    elif 'around' in action or 'room' in action or 'apartment' in action:
        print('onee')
    elif 'david' in action or 'tyler' in action or 'boy' in action or 'brat' in action or 'youth' in action:
        print('onef')
        break
    else:
        print("You can't see that")

It prints 'onec' for any input beginning with 'look'.

Upvotes: 1

Views: 169

Answers (4)

Caleb Hattingh
Caleb Hattingh

Reputation: 9225

  1. The while statement requires a condition.
  2. You can call the same instructions over and over using a function.
  3. "String literals can span multiple lines in several ways"
  4. Use strategically-placed print statements to show the value of action, e.g. after if action[0]=='look'

Lastly, please don't add any more items to this question. Rather ask a new question. This site has somewhat specific rules on that sort of thing.

Upvotes: 2

reem
reem

Reputation: 7236

  1. while requires a condition that it has to evaluate. If you want it to loop forever, just give it a condition that always evaluates to True, such as 4>3. It would be best for everyone if you just used while True:, which is the clearest option.
  2. For this specific case, I would recommend using a dict() and its .get() method. Something like this:

    action_dict = {'go':"You're supposed to go to David!", 
                   'look':"You can't see that",
                   'take':"You don't see the point in taking that."  
                  }
    print(action_dict.get(action[0], "I don't recognise that command")
    

    would replicate what you have going on right now.

  3. See the link provided by cjrh here: http://docs.python.org/3.3/tutorial/introduction.html#strings
  4. Our mind-reading powers are a bit off in October, we'll need some more information other than "it does not work" to help you with that.

Upvotes: 0

aIKid
aIKid

Reputation: 28292

  1. Just register the string first, then when the input come, change it:

     command = "nothing"
     command = input("Enter command: ")
     while command:
    

    Or just simply:

     while True:
    
  2. Yes, think about it by yourself.. Okay, why not put it in list responses?

  3. If it is really long, put it in a file. Read it when you need it using open(). More on File Processing This will help you shorten your code, making it easier to read, and makes it more efficient.

Upvotes: 0

SoItBegins
SoItBegins

Reputation: 534

  1. To make an infinite While loop, use while True:.

  2. You could use a dict to store common action strings and their responses.

Upvotes: 0

Related Questions