dimanche 8 mars 2020

Specific if statement not working after gathering multiple lines of input

Im having a bit of trouble with this python problem, so the first part of the task is to basically read from multiple lines of input until a blank line is entered.

If the first word is 'Capture', there will be two words following it: the name of the Pokémon (e.g. Pikachu) and its current level (e.g. 5) so it would look something like this: "Capture Pikachu 5"

If the first word is Query, the next word will be the name of a Pokémon. Your program should print the level of the Pokémon that you have previously captured, or it should warn the user if it is not tracking that Pokémon name. For example, if I typed "Capture Pikachu 5" previously, and then wrote "Query Pikachu" in the next line of input, it would return "Pikachu is level 5." If I wrote "Query Eevee" instead of "Query Pikachu", it would instead say "You have not captured Eevee yet".

If I try to "Capture" a Pokémon that has already been captured, (e.g. I write "Capture Pikachu 5" then in the next line I write "Capture Pikachu 4") it will say "You are already training Pikachu!"

Finally, if I enter any unknown command apart from "Capture" and "Query", then it will say "Unknown Command!"

Heres my code so far:

while line:
  if 'Capture' in line:   
    parts = line.split()
    name = parts[1]
    number = parts[2]
    line = input('Command: ')
  elif 'Query' in line and parts[1] in line:
    print(name, 'is level', number + '.')
    line = input('Command: ')
  elif 'Capture' in line and parts[1] == name:
    part3 = line.split()
    notname2 = part3[1]
    print('You are already training', notname2 + '!')
    line = input('Command: ')
  elif 'Query' in line and parts[1] not in line:
    part2 = line.split()
    notname = part2[1]
    print('You have not captured', notname, 'yet.')
    line = input('Command: ')
  else:
    print('Unknown command!')
    line = input('Command: ')

Everything seems to work except for when you try to capture a Pokémon that has already been captured. For example, if I wrote "Capture Eevee 3" then wrote "Capture Eevee 5", I want it to say "You are already training Eevee!" but instead it does not say anything and just asks for the next line of input.

I'm not exactly sure what the problem in my code is, as everything else seems to work perfectly fine, any help would be greatly appreciated. Thanks! :)

Aucun commentaire:

Enregistrer un commentaire