比较两个数组中的精确值

时间:2018-10-23 08:23:03

标签: python arrays compare comparison

所以这有点令人困惑。我目前正在制作王牌游戏。现在我有两个阵列,一个玩家卡座和一个计算机卡座。它的工作方式是从狗的名字的文本文件中创建一个数组,然后为其分配4个值(运动,智力,友善和口水)。所有这一切。然后,它拆分套牌,为每个阵列(播放器和计算机)分配一半。用户获得了第一选择权,而我设法将其允许用户选择一个类别。我不知道该怎么做是比较两个数组中的确切值。数组如下所示(示例):

[['Fern-the-Fox-Terrier'],'Exercise:',3,'Intelligence:',67,'Friendliness:',10,'Drool:',4]

以下是您需要的代码:(我不确定如何附加文本文件)

import random
import shutil
import os      #here I have imported all necessary functions for the following code to work
import array
import time

allowedresponses = ["Exercise","Intelligence","Friendliness","Drool"] #this allows me to make sure both the user and the computer select only the values available
cardcount = 0
usercards = 0           #the following variables are used for later
computercards = 0
x = 1
y = 0


play = input("Welcome to Celebrity Dogs Top Trumps! Press S to start, or Q to quit: ")
play = play.upper()
while x==1:
    if play == "S" or play == "s":
        print("Great! Lets play!")
        x+=1                                #here the user is given the option to play the game or quit. Pressing Q will immediatley end the the program, whilst pressing S will start it.
    elif play == "Q" or play == "q":        #x = 1 variable is used to end the while loop, permitted the user enters an appropriate answer
        print("OK, bye")
        quit
        x+=1
    else:
        print("That's not a valid answer. Please try again")
        play = input("Welcome to Celebrity Dogs Top Trumps! Press S to start, or Q to quit: ")

cardcount = int(input("How many cards in this game? Please pick an even number between 4 and 30: "))  #the following section of code asks the to select the number of cards they want played
while x==2:
    if cardcount < 4:
        print("Thats too small! Please try again!") #the programs tells the user to select again if they have picked a number smaller than 4
        cardcount = int(input("How many cards in this game? Please pick an even number between 4 and 30: "))
    elif cardcount > 30:
        print("Thats too big! Please try again!") #the programs tells the user to select again if they have picked a number larger than 30
        cardcount = int(input("How many cards in this game? Please pick an even number between 4 and 30: "))
    if cardcount % 2 != 0:
        print("Thats an odd number! Please try again!")#the programs tells the user to select again if they have picked a number that can't be divided by 2 with no remainders, essentially forcing only even numbers
        cardcount = int(input("How many cards in this game? Please pick an even number between 4 and 30: "))
    else:
        print("Perfect! Onwards!")
        x+=1 #once again I have used the x variable to allow the user to continue if they have selected a number that fits the requirements

time.sleep(1)

print("----------------------------------------------------------------------")

print("Rules: 1) For exercise, intelligence and friendliness, the highest value wins. For drool, lowest wins")
print("       2) If there is a draw, the player that picked the value wins and gets the cards")
shutil.copyfile('dogs.txt','celebdogs.txt') #this creates a copy of the dogs text file. It is mainly for if I ever need to edit a file with the dogs in,, the origin file is never changed.

topdogs = [] #here I have created an array for the dogs 
inp = open('celebdogs.txt','r') #I have used python's text file edits to import everything in the newly made celebdogs text file into an array, in order to import it later
for line in inp.readlines():
    topdogs.append([])
    for i in line.split():
        topdogs[-1].append(i)
inp.close()

deck = [] #here I have created an array for the deck of cards that will be used in the game

print("----------------------------------------------------------------------")

for index in range(0,cardcount): #this part of the code tells the program to repeat the following until the number of cards selected by the user is in the deck
    deck.insert([index][0], [(random.choice(topdogs))])#h
    deck[index].append("Exercise: ")
    deck[index].append(random.randint(0,5))
    deck[index].append("Intelligence: ")
    deck[index].append(random.randint(0,100))
    deck[index].append("Friendliness: ")
    deck[index].append(random.randint(0,10))
    deck[index].append("Drool: ")
    deck[index].append(random.randint(0,10))

time.sleep(1)

playerDeck=[]
computerDeck=[]

while len(deck)>0:
    playerDeck.append(deck.pop(0))
    computerDeck.append(deck.pop(0))

time.sleep(1)

print("This is your deck: ")
print(playerDeck)

playerTurn = True

print("----------------------------------------------------------------------")

time.sleep(1)

print("This is your first card: ")
print(playerDeck[0])

if playerTurn == True:
    answer = input("Please select an attack (Exercise, Intelligence, Friendliness and Drool): ")
    while allowedresponses.count(answer) == 0:
        answer = input("That isn't a valid choice, please try again: ")

else:
    answer = random.choice(allowedresponses)
    print("Computer chooses", answer)

print("Computer Card: ")
print(computerDeck[0])


if playerDeck == cardcount:
    print("You win!!!!")

if computerDeck == cardcount:
    print("Computer wins!!!!")

os.remove('celebdogs.txt')

以防万一,我还需要了解如何比较两个数组中的精确值,因此如果我想比较单个卡片在两个卡片组中的“锻炼”值,就需要帮助。这两个数组的格式完全相同(名称,锻炼,价值,智力,价值,友善度,价值,流口水,价值),因此我需要能够比较特定的值。

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用python的zip函数:

computerDeck = [1, 2, 3]
playerDeck = [1, 3, 3]

for computer_card, player_card in zip(computerDeck, playerDeck):
    if computer_card == player_card:
        print('valid')
    else:
        print('invalid')