Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中基于随机字符串执行if语句?_Python_Variables_If Statement_Random_Statements - Fatal编程技术网

如何在Python中基于随机字符串执行if语句?

如何在Python中基于随机字符串执行if语句?,python,variables,if-statement,random,statements,Python,Variables,If Statement,Random,Statements,然后如何根据呈现的随机字母对print_消息(“This…”)的结果执行if语句 例如。如果代码[0]的结果为print_message()的“A”,则您将在屏幕上看到以下内容: codes = ["A", "B", "C", "D", "E"] random.shuffle(codes) def print_message(message): print "\n" print "-"*10 print message p

然后如何根据呈现的随机字母对print_消息(“This…”)的结果执行if语句

例如。如果代码[0]的结果为print_message()的“A”,则您将在屏幕上看到以下内容:

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])
再运行几次命令,您会看到:

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

我会使用字典,使用代码(“a”、“B”、“C”)作为字典键,并将“message”放入dict值中

----------
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. 
----------
注意:@mgilson指出,对于Python3.x,
random.choice
需要一个列表,因此您可以这样做:

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])

我会使用字典,使用代码(“a”、“B”、“C”)作为字典键,并将“message”放入dict值中

----------
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. 
----------
注意:@mgilson指出,对于Python3.x,
random.choice
需要一个列表,因此您可以这样做:

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])

这将为您提供问题中示例的结果:

random_key = random.choice(list(codes.keys()))

这将为您提供问题中示例的结果:

random_key = random.choice(list(codes.keys()))

这与
if
有什么关系?我不明白。你要做的就是打印不同的消息,还是还有更多?不同的消息可以由字典处理。这与if有关,因为如果结果是打印特定于a的消息。我尝试了一个字典,但它不适用于我的目的。字典没有在第一行的正下方输出第二行。有没有办法做到这一点?这与
if
有什么关系?我不明白。你要做的就是打印不同的消息,还是还有更多?不同的消息可以由字典处理。这与if有关,因为如果结果是打印特定于a的消息。我尝试了一个字典,但它不适用于我的目的。字典没有在第一行的正下方输出第二行。有办法吗?啊,看起来差不多。我试试看是否适合我。我是一个初学者,所以这很有帮助。传递
code.keys()
对python3.x有用吗。我的印象是,
random.choice
要求输入是一个
列表
@mgilson,但我认为不是这样。对于python3.x,另一种选择可能是
random.choice(list(code.keys())
。它在py3k上会中断。它要求输入是一个“序列”--尽管它可能只使用带有整数的
\uuuu getitem\uuuu
。。。但是,是的,您需要
list(codes.keys())
使其与py3k兼容。我不会提到它,除非你的打印声明也可以与py3k一起使用,所以…好的观点是:打印声明。有时我脑子里会把蟒蛇2和3搞混!啊,看起来差不多对了。我试试看是否适合我。我是一个初学者,所以这很有帮助。传递
code.keys()
对python3.x有用吗。我的印象是,
random.choice
要求输入是一个
列表
@mgilson,但我认为不是这样。对于python3.x,另一种选择可能是
random.choice(list(code.keys())
。它在py3k上会中断。它要求输入是一个“序列”--尽管它可能只使用带有整数的
\uuuu getitem\uuuu
。。。但是,是的,您需要
list(codes.keys())
使其与py3k兼容。我不会提到它,除非你的打印声明也可以与py3k一起使用,所以…好的观点是:打印声明。有时我脑子里会把蟒蛇2和3搞混!“臭得像猴子球”。出售!“臭得像猴子球”。出售!