从一个列表中删除一个项目并将其存储到另一个列表的末尾

时间:2021-06-20 08:03:45

Hello I am making a simple card game in python in which players compare a stat of a card, the card with the higher stat value wins the round and the loser gives them that card, removing it from their deck and putting it into the end of the winners deck.

你好我在python中制作一个简单的纸牌游戏,其中玩家比较一张卡的统计数据,具有较高stat值的卡赢得该轮,而输家给他们那张卡,将其从他们的牌组中移除并将其放入最后获胜者甲板。

Here is my code for the game which includes a round of gameplay:

这是我的游戏代码,包括一轮游戏:

import random
   class Ram():
        def __init__(self, name, weight, milk, wool, offs, thigh, fert, meat, fat):
           self.name = name
           self.weight = weight
           self.milk = milk
           self.wool = wool
           self.offs = offs
           self.thigh = thigh
           self.fert = fert
           self.meat = meat
           self.fat = fat

       def __repr__(self):
           return f"{self.name,self.weight,self.milk,self.wool,self.offs,self.thigh,self.fert,self.meat,self.fat}"


def Read(txtfile):
    datalist=[]
    f = open(txtfile, "r", encoding="utf-8")
    for line in f:
        datalist.append(line.split(','))
    f.close()
    cardlist = []
    for i in datalist:
        cardlist.append(Ram(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]))
    return cardlist

def RandomChoice():
   l=["User","Computer"]
   return random.choice(l)

 cardlist=Read("hrutaspil.txt")

 random.shuffle(cardlist)
 split = len(cardlist) // 2

user_deck = cardlist[:split]
computer_deck = cardlist[split:]


game=input("Do you want to play?(y/n)")
if game == "y":
    if RandomChoice() == "User":
       print("The user chooses first")
       print("Your top card is:",user_deck[0].name)
       print("1 = weight =",user_deck[0].weight)
       print("2 = milk =", user_deck[0].milk)
       print("3 = wool =", user_deck[0].wool)
       print("4 = offs =", user_deck[0].offs)
       print("5 = thigh =", user_deck[0].thigh)
       print("6 = fert =", user_deck[0].fert)
       print("7 = meat =", user_deck[0].meat)
       print("8 = fat =", user_deck[0].fat)
       choice=input("Choose stat: ")
       if choice == ("1"):
          print("Weight is:",float(user_deck[0].weight))
          print("compared with computer:",float(computer_deck[0].weight))
          if float(computer_deck[0].weight) > float(user_deck[0].weight):
             print("Computer wins.")
          else:
             print("You win.")

Now, how do I remove a card from the losers deck and add it to the bottom of the winners deck?

现在,如何从失败者牌组中取出一张牌并将其添加到获胜者牌组的底部?

1 个解决方案

#1


0  

if float(computer_deck[0].weight) > float(user_deck[0].weight):
    print("Computer wins.")
    computer_deck.append(user_deck.pop(0))
else:
    print("You win.")
    user_deck.append(computer_deck.pop(0))

Are you also aware that the user wins ties? I'm not sure that's what you intended based on reading your question.

你是否也意识到用户赢得了联系?基于阅读你的问题,我不确定你的意图是什么。

#1


0  

if float(computer_deck[0].weight) > float(user_deck[0].weight):
    print("Computer wins.")
    computer_deck.append(user_deck.pop(0))
else:
    print("You win.")
    user_deck.append(computer_deck.pop(0))

Are you also aware that the user wins ties? I'm not sure that's what you intended based on reading your question.

你是否也意识到用户赢得了联系?基于阅读你的问题,我不确定你的意图是什么。