【发布时间】:2013-01-25 18:40:35
【问题描述】:
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])
然后我如何根据出现的随机字母为 print_message('This...') 的结果执行 if 语句。
示例。如果代码 [0] 的结果最终是 print_message() 的“A”,那么您会在屏幕上看到这个打印:
----------
This is a test of the A system.
The A system is really great.
---------
再运行几次命令,你会看到:
----------
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.
----------
【问题讨论】:
-
这与
if有什么关系?我不明白。 -
您想要做的只是打印不同的消息还是还有更多?不同的消息可以由字典处理。
-
它与 if 有关,因为如果结果是 A,则打印特定于 A 的消息。我尝试了字典,但它不适合我的目的。字典没有在第一行正下方输出第二行。有没有办法做到这一点?
标签: python variables if-statement random statements