0

If I want to define a sequence results of the form PIPPPINNNIPPPPPPPPPPINN

for the following planets elements: AXDDCCCCSSDFQWEAACCCXXX, porty: ASDF, y, Nick: ZXCV

Where P and N are the initials of the player leading the other, and I is the positioner when both players are tied.

The code with which you started the resolution is this:

porty = input('').upper()
nick = input('').upper()
planetas = input('').upper()
resultado=""
cont1=0
cont2=0
for resultado in planetas:
  for n in nick:
    if resultado == n:
      cont1 = cont1+1
for resultado in planetas:      
  for p in porty:
    if resultado ==p:
      cont2 = cont2+1

    if (cont1 > cont2):
      resultado +="P"
    else:
      if (cont1 > cont2):
        resultado +="N"
      else: 
        resultado +="I"
print(resultado) 
2
  • resultado is used both as a loop variable and as the final result. I think you should use a separate loop variable (planeta?). The way this is written resultado is overwritten each time through the loop. Commented Jun 17, 2021 at 13:17
  • Also it would help if you said what the problem is you are experiencing. Do you get the wrong output? If so what is it? Does the loop not terminate? Commented Jun 17, 2021 at 13:18

1 Answer 1

1

The sequence that you expect does not match the inputs you provided, but here is a solution nonetheless :

player1_letters = "ASDF"
player2_letters = "ZXCV"
planets = "AXDDCCCCSSDFQWEAACCCXXX"

result = ""
for planet in planets:
    if planet in player1_letters:
        result += "P"
    elif planet in player2_letters:
        result += "N"
    else:
        result += "I"

assert result == "PNPPNNNNPPPPIIIPPNNNNNN", result
# example by OP : PIPPPINNNIPPPPPPPPPPINN
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.