比较两个数组中的精确值



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

[['狐狸梗蕨'], '锻炼: ', 3, '智力: ', 67, '友好: ',10, '流口水: ', 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')

以防万一我没有很好地解释这一点,我需要帮助知道如何比较两个数组中的精确值,所以如果我想比较两副牌中单张牌的锻炼值。两个数组具有完全相同的格式(名称、练习、值、智能、值、友好性、值、流口水、值(,因此我需要能够比较特定值。

谢谢!

尝试使用 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')

相关内容

  • 没有找到相关文章

最新更新