JUMBLE WORDS ,我希望一旦这个词被问到任何玩家都不应该重复
Posted
技术标签:
【中文标题】JUMBLE WORDS ,我希望一旦这个词被问到任何玩家都不应该重复【英文标题】:JUMBLE WORDS ,I want that if once the word is asked to any player should not repeat 【发布时间】: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()
【参考方案1】:
为了不重复单词添加下面的代码。它将维护一个到目前为止已经选择的单词的列表,如果它已经被使用,那么它将再次去选择直到它得到未使用的单词。
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
chosen_words = []
while (1):
# computer will give question to players picked words
while (1):
picked_word = choose()
if picked_word in chosen_words:
pass
else:
chosen_words.append(picked_word)
break
# now create the question
Q = jumble(picked_word)
print(Q)
单词一旦被问到任何玩家就不会重复 输出:
Connected to pydev debugger (build 193.6494.30)
player 1, Please enter your name Pooja
player 2, Please enter your name Meera
rchimi
Pooja your turn.
What's in your mind
mirchi
your score is : 1
ecnscie
Meera your turn.
What's in your mind
science
your score is : 1
ayeprl
Pooja your turn.
What's in your mind
player
your score is : 2
stcithmmaa
Meera your turn.
What's in your mind
mathmatics
your score is : 2
mharreeakts
Pooja your turn.
What's in your mind
sharemarket
your score is : 3
waret
Meera your turn.
What's in your mind
water
your score is : 3
noiitcdno
Pooja your turn.
What's in your mind
condition
your score is : 4
ndiueocta
Meera your turn.
What's in your mind
education
your score is : 4
gammun
Pooja your turn.
What's in your mind
magnum
your score is : 5
uertocpm
Meera your turn.
What's in your mind
computer
your score is : 5
servere
Pooja your turn.
What's in your mind
reverse
your score is : 6
nogma
Meera your turn.
What's in your mind
mango
your score is : 6
iraownb
Pooja your turn.
What's in your mind
rainbow
your score is : 7
abdor
Meera your turn.
What's in your mind
board
your score is : 7
【讨论】:
以上是关于JUMBLE WORDS ,我希望一旦这个词被问到任何玩家都不应该重复的主要内容,如果未能解决你的问题,请参考以下文章