0
codes = ["A", "B", "C", "D", "E"]
random.shuffle(codes)

    def print_message(message):
        print "\n"
        print "-"*10
        print message
        print "-"*10
        print "\n"

    print_message('This is a test of the %s system' % codes[0])

How do I then do an if statement for the results of the print_message('This... ') based on the random letter that is presented.

Example. If the result of codes[0] ended up being "A" for the print_message() then you would see this printed on the screen:

----------
This is a test of the A system. 
The A system is really great. 
---------

Run the command a few more times and you'd see:

----------
This is a test of the C system.
The C system sucks. 
----------

----------
This is a test of the B system. 
The B system has improved greatly over the years. 
----------
Eric
  • 95,302
  • 53
  • 242
  • 374
Kryten
  • 595
  • 3
  • 10
  • 25
  • 2
    How is this related to an `if`? I don't get it. – phant0m Jan 25 '13 at 18:42
  • All you want to do is print distinct messages or is there more ? Distinct messages can be handled by a dictionary. – asheeshr Jan 25 '13 at 18:43
  • It's related to if because if the result is A print a message specific to A. I tried a dictionary and it didn't work for my purposes. The dictionary didn't output the secondary line right below the first line. Is there a way to do that? – Kryten Jan 25 '13 at 18:48

2 Answers2

3

I would use a dictionary, and use the codes ("A", "B", "C") as the dictionary keys, and put the "message" into the dict value.

codes = {
    'A': 'The A system is really great.',
    'B': 'The B system has improved greatly over the years.',
    'C': 'The C system sucks.'
}

random_key = random.choice(codes.keys())
print("This is a test of the %s system" % random_key)
print(codes[random_key])

Note: As @mgilson pointed out, for python 3.x, random.choice requires a list, so you can do this instead:

random_key = random.choice(list(codes.keys()))
Nitzle
  • 2,417
  • 2
  • 22
  • 23
  • Ah, that looks about right. I'll try that and see if its right for me. I am a beginner so this helps a lot. – Kryten Jan 25 '13 at 18:52
  • Does passing `codes.keys()` work on python3.x. I was under the impression that `random.choice` required the input to be a `list` – mgilson Jan 25 '13 at 19:08
  • @mgilson I don't think it does. An alternative could be `random.choice(list(codes.keys()))` for python3.x. – Nitzle Jan 25 '13 at 19:09
  • It does break on py3k. It requires that the input be a "sequence" -- Although it probably only uses `__getitem__` with integers ... but yeah, you need `list(codes.keys())` to make it py3k compatable. I wouldn't have mentioned it except your print statement is also workable with py3k so ... – mgilson Jan 25 '13 at 19:10
  • Good point re: the print statement. I get python2 and 3 mixed up in my head sometimes! – Nitzle Jan 25 '13 at 19:12
  • @Nitzle -- For some reason I always try really hard to support python2.x and 3.x in the same source file. It makes you pretty aware of the subtle differences which is cool. – mgilson Jan 25 '13 at 19:13
1

This would give you the results of the examples in your question:

#! /usr/bin/env python
import random
codes = ["A", "B", "C", "D", "E"]
random.shuffle(codes)

def print_sys_result(sys_code):
    results = {
        "A": "is really great",
        "B": "has improved greatly over the years",
        "C": "sucks",
        "D": "screwed us up really badly",
        "E": "stinks like monkey balls"
    }
    print "\n"
    print "-"*10
    print 'This is a test of the {0} system'.format(sys_code)
    if sys_code in results:
        print "The {0} system {1}.".format(sys_code, results[sys_code])
    else:
        print "No results available for system " + sys_code
    print "-"*10
    print "\n"

print_sys_result(codes[0])
crayzeewulf
  • 5,840
  • 1
  • 27
  • 30