lundi 29 octobre 2018

Python--changing number of conditions inside if statement depending on input

So for fun I've been making the following code that essentially takes in two Pokemon, and outputs a list of Pokemon that would round out a three Pokemon team based on defenses. I have a full working code for all 18 types, but here's a smaller version I've been using for testing:

import numpy as np
import sys

#testing assuming input in fire
dict={}
dict["fire"]=np.array([1,-1,1]) 
dict["water"]=np.array([1,1,-1])
dict["grass"]=np.array([-1,1,1])

value={}
value[0]="fire"
value[1]="water"
value[2]="grass"

type1=input("First type? (no caps): ")
type(type1)
type2=input("Second type? (no caps): ")
type(type2)

poke=dict[type1]+dict[type2]

for t in range(len(poke)):
    if poke[t]<0:
        poke[t]=-1
    elif poke[t]>0:
        poke[t]=1
    else: continue

A=np.column_stack(([fire,water,grass]))

weak=any(poke<0)
p=np.linspace(0,0,len(poke))
if weak==True:
    for t in range(len(poke)):
        if poke[t]<0:
            p[t]=1
        else:
            p[t]=0
else:
    print("....your pokemon has no weaknesses....")

for u in range(0,len(poke)):
    for v in range(u,len(poke)):
        combo=A[:,u]+A[:,v]
        for t in range(0,len(poke)):
            if combo[t]<0:
                combo[t]=-1
            elif combo[t]>0:
                combo[t]=1
            else: continue
        if combo[1]==-poke[1]: #dep on values of p that are not 0
            if u==v:
                print(value[u])
            else:
                print(value[u],value[v])
        else: continue

What the code basically does is I have a series of arrays representing each type. A 1 means it resists the pokemon corresponding to that type, a -1 is weak to it, and a 0 is neutral. In the full code it accepts two dual type pokemon, adds together their arrays, and I treat it as a single entity. In the smaller code I'm taking in one and looking for one. After anything is added together I change the values back to 1's, and 0's so I can do proper comparisons.

And then the actual comparison is done by this double nested for loop that goes over every possible combination and compares it to want I want, which is basically for any value of poke[ ] that is -1, I want a matching combo[ ] value that is 1.

if combo[1]==-poke[1]:
        if u==v:
            print(value[u])
        else:
            print(value[u],value[v])
    else: continue

The issue is that I'm having trouble generalizing the code so I don't have to go back and edit it every time I want to do a new "search".

For this instance of the code:

>>First type? (no caps): fire
    Second type? (no caps): fire
    water
    water grass
    grass 

But, if I wanted to do, say a water type:

>>First type? (no caps): water
    Second type? (no caps): water
    fire

It should suggest fire, fire/grass, grass, but it doesn't because the if statement no longer points to the correct index to check. This is something I edit every time and am having trouble with when it comes to generalizing to simply input/output.

I made this value p which is an array of 0's and 1's, where 1 is a weakness and 0 is anything else:

 weak=any(poke<0)
    p=np.linspace(0,0,len(poke))
    if weak==True:
        for t in range(len(poke)):
            if poke[t]<0:
                p[t]=1
            else:
                p[t]=0
    else:
        print("....your pokemon has no weaknesses....")

I think I could probably make a way to check the values where p=1 specifically, but then the actual problem occurs. In nearly all cases there's more than one weakness, which leads to having to check 3 or 4 four conditions in the same if statement like:

if combo[1]==-poke[1] && combo[5]==-poke[5] && combo[13]==-poke[13]:

And even then that combination sometimes doesn't exist. I was thinking of printing out the weaknesses corresponding to p=1 if it's more than 3 and having some sort of user input on what they would like to check, but I can't figure out how to have the number of conditions in a single if statement vary like that. I'm also unsure how to make the current single statement point to the p indexes like I want.

I will continue to work on this but I appreciate any help I can get since I'm still pretty new.

Thank you!

Aucun commentaire:

Enregistrer un commentaire