如何让python从列表中随机选择一些内容,如果在输入提示符中键入,它将按照预期显示确切的答案

2024-05-29 07:18:18 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我想做一个类似我们之间的游戏,但不同,只是为了好玩 不管怎样,每当我试图使它工作,它会说我想要的信息,无论什么。 代码如下:

import turtle   
t=turtle.Turtle()  
import random       

imposteris = ['red', 'blue', 'green', 'cyan', 'lime', 'black', 'white', 'purple', 'orange', 'brown']  
random.choice("imposteris")  
n = 1

print("Welcome to among them, you are a detective, figure out who the imposter is")   
meeting = int(input("Press 1 if you want to call a meeting"))   
if meeting:   
  input("A meeting has been called! Who do you think the imposter is?, Do either red, blue, green, cyan, lime, black, white, purple, orange, brown")   
  for i in range(n):   
    if random.choice(imposteris):   
      print("Congratulations, You caught the imposter.")      
    else:   
      print("He was a crewmate :/")

 

Tags: theimportyouifrandomgreenbluered
2条回答

为了测试代码,我取出了您导入的海龟模块,但您可以稍后再放回去

import random

imposters = ['red', 'blue', 'green', 'cyan', 'lime', 'black', 'white', 'purple', 'orange', 'brown']

print("Welcome to among them, you are a detective, figure out who the imposter is\n")
print("Press 1 if you want to call a meeting")
meeting = int(input("> "))
if meeting:
    imposter = imposters[random.randint(0, len(imposters) - 1)]
    print(imposter)
    print("A meeting has been called! Who do you think the imposter is?, Do either red, blue, green, cyan, lime, black, white, purple, orange, brown")
    imposterGuess = str(input("> "))
if imposter == imposterGuess:
    print("Congratulations, You caught the imposter.")
else:
    print("%s was a crewmate :/" % imposterGuess)```


您必须将一个变量链接到随机选择,因此在本例中:your_variable = random.choice(imposteris) 然后可以对变量执行任何操作,例如:print(your_variable)

相关问题 更多 >

    热门问题