【发布时间】:2021-07-30 01:43:17
【问题描述】:
我不想和一个玩家重复这个词
我正在创建混乱的文字游戏,但问题是曾经使用过的单词一次又一次地重复,所以我应该怎么做才能避免它
请说明该怎么做 我也尝试使用 del 但它没有成功,我也尝试了所有流行但仍然无法执行 请建议
import random
def choose():
words=["rainbow","computer","science","mathmatics","player","condition","water","reverse","board","education","sharemarket","mango","magnum","mirchi"]
pick=random.choice(words) #to choose random words we have used random library
return pick
def jumble(word):
jumbled="".join(random.sample(word,len(word))) #join function is used to join words together,also random.sample word
return jumbled #randmoly select the word
def thank(p1name,p2name,points_p1,points_p2):
print(p1name,"your score is :", points_p1)
print(p2name,"your score is :", points_p2)
print("THANKS FOR PLAYING\n Have a nice day!!!!!!!")
def play():
p1name=input("player 1, Please enter your name ")
p2name=input("player 2, Please enter your name ")
points_p1=0
points_p2=0
turn=0
while(1):
#computer will give question to players picked words
picked_word=choose()
#now create the question
Q=jumble(picked_word)
print(Q)
#PLAYER 1
if turn%2==0:
print(p1name,"your turn. ")
answer=input("What's in your mind\n")
if answer==picked_word:
points_p1=points_p1+1
print("your score is :" , points_p1)
else:
print("better luck next time", picked_word)
c=int(input("press 1 to continue and 0 to quit"))
if c==0:
thank(p1name,p2name,points_p1,points_p2)
break
#player 2
else:
print(p2name,"your turn. ")
answer=input("What's in your mind\n")
if answer==picked_word:
points_p2=points_p2+1
print("your score is :" , points_p2)
else:
print("better luck next time", picked_word)
c=int(input("press 1 to continue and 0 to quiet "))
if c==0:
thank(p1name,p2name,points_p1,points_p2)
break
turn=turn+1
play()
【问题讨论】:
-
签出
random.shuffle()
标签: python-3.x list visual-studio solver solution