[英]How do I make python randomly select something from a list and if typed in the input prompt, it will show the exact answer as intended
基本上,我想制作一款類似於我們中間的游戲,但又有所不同,只是為了好玩。無論如何,無論何時我試圖讓它發揮作用,無論如何它都會說出我想要的信息。 這是代碼:
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 :/")
您確實必須將變量鏈接到您的隨機選擇,因此在這種情況下: your_variable = random.choice(imposteris)
然后您可以對變量執行任何操作,例如: print(your_variable)
我取出了您導入的 Turtle 模塊以測試代碼,但您可以稍后再放回去。
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)```
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.