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. 
----------
3
  • 2
    How is this related to an if? I don't get it. Commented Jan 25, 2013 at 18:42
  • All you want to do is print distinct messages or is there more ? Distinct messages can be handled by a dictionary. Commented Jan 25, 2013 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? Commented Jan 25, 2013 at 18:48

2 Answers 2

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()))
Sign up to request clarification or add additional context in comments.

6 Comments

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.
Does passing codes.keys() work on python3.x. I was under the impression that random.choice required the input to be a list
@mgilson I don't think it does. An alternative could be random.choice(list(codes.keys())) for python3.x.
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 ...
Good point re: the print statement. I get python2 and 3 mixed up in my head sometimes!
|
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])

1 Comment

"stinks like monkey balls". Sold!

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.