简体   繁体   中英

How to create a dictionary from 2 lists and other dictionary in Python?

Answer here .

1.First I created variables POWER , gandalf and saruman as seen above in the code then a variable called spells to store the number of spells that the sorcerers cast.

code

POWER = {
    'Fireball': 50, 
    'Lightning bolt': 40, 
    'Magic arrow': 10, 
    'Black Tentacles': 25, 
    'Contagion': 45
}

gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', 
           'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']
saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', 
           'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']
spells=10

  1. Then created two variables called gandalf_wins and saruman_wins. Set both of them to 0.
gandalf_wins=0
saruman_wins=0
  1. And Lastly two variables called gandalf_power and saruman_power to store the list of spell powers of each sorcerer.
gandalf_power=[]
saruman_power=[]
  1. The battle starts, Using the variables you've created above. code the execution of spell clashes. Remember that a sorcerer wins if he succeeds in winning 3 spell clashes in a row, If a clash ends up in a tie. the counter of wins in a row is not restarted to 0. Remember to print who is the winner of the battle.

I am stucked here because I don't know how to create a dictionary for each with the spells and the power of this spells. Then, how should I compare them? Thanks in advance!

I think the steps are instructed pretty clear, using a for loop is enough to solve the 3rd part.

For the 4th part, you can use zip , to iterrate both list at the same time.

for gandalf_pow, saruman_pow in zip(gandalf_power, saruman_power):
    # compare

The below is using zip to loop through the spells and compares each rounds spell power.

POWER = {
    'Fireball': 50, 
    'Lightning bolt': 40, 
    'Magic arrow': 10, 
    'Black Tentacles': 25, 
    'Contagion': 45
}

gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']
saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']
spells=10

gandalf_wins=0
saruman_wins=0

for gandalf_spell, saruman_spell in zip(gandalf,saruman): 
    if POWER[gandalf_spell] > POWER[saruman_spell]: 
        gandalf_wins+=1 
    elif POWER[saruman_spell] > POWER[gandalf_spell]: 
        saruman_wins+=1 

print(f"Gandalf won {gandalf_wins} times, Saruman won {saruman_wins} times")

you should refer Python Dictionaries. They are just like lists and tuples only the difference is that they are like database's table where you have rows and columns for storing different values

https://www.w3schools.com/python/python_dictionaries.asp Refer this for help

I don't think you need a dictionary for this. You can try:

for x in range(spells):
    gandalf_attack = POWER[gandalf[x]]
    saruman_attack = POWER[saruman[x]]
    if gandalf_attack>saruman_attack:
        # Do something
    elif gandalf_attack>saruman_attack:
        # Do something
    else:
        # Tie

Finally, I did this and worked for me:

for spell in gandalf:
    gandalf_power.append(POWER[spell])

for spell in saruman:
    saruman_power.append(POWER[spell])

winner = ""

for g, s in zip(gandalf_power, saruman_power):    
    if g > s: 
        gandalf_wins+=1
        saruman_wins=0
    elif s > g: 
        saruman_wins+=1
        gandalf_wins=0

    if gandalf_wins==3:
        winner="Gandalf"
        break
    elif saruman_wins==3:
        winner="Saruman"
        break

if gandalf_wins < 3 and saruman_wins < 3:
    print("Tie.")
else:
    print(f"The winner is {winner}!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM