-3

I am unable to change the name of my Knight; however I am able to change the armour and weapon. I am very new to python so please bear with me, when I enter a new name try again comes up. Thank you,

def update_data(knights, weapon, armour):
  print("What would you like to update ?")
  print("1: Knights name: " + knights[0])
  print("2: Weapon: " + weapon[0])
  print("3: Armour: " + armour[0])
  

  try:   
    selection = int(input("Select your Option: "))
 
    if selection == 1:
        knights[0] = str(input("what is their new name ? " + knights[0]))
        print("Your Knights new name is: " + knights[0])
    elif selection == 2:
        weapon[0] = str(input("What is their new weapon ? " + weapon[0]))
        print("They are now armed with: " + weapon[0])
    elif selection == 3:
        armour[0] = str(input("What new armour shall they wear ? " + armour[0]))
        print("They are now wearing: " + armour[0])
  except:
    print("---Try Again---")
    update_data(knights, weapon, armour)
BLVMF
  • 1
  • 2
  • Can you tell what kind of error is being thrown? Perhaps `knights`, `weapon`, or `armor` is empty. – Green Cloak Guy Jun 12 '22 at 22:33
  • 4
    change your `except:` for `except Exception as exc:` and in bellow it include the following print: `print("got exception:",exc)` with that you can see you can see what exception you're getting and start the debugging process – Copperfield Jun 12 '22 at 22:37
  • 4
    You except is a bare exception which is considered bad practice i.e. [What is wrong with using a bare 'except'?](https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except) – DarrylG Jun 12 '22 at 22:41
  • also take a look at this: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Copperfield Jun 12 '22 at 22:44
  • "How do I fix bare exceptions?" is the wrong question, because you should never use bare exceptions in the first place; especially ones with `except` blocks that don't log what they caught. The only right answer to "how do I fix not being able to see the error when I use an `except:` block that doesn't print the error?" is "don't do that at all". When you expect a _specific_ error that you want to silently suppress, you should catch _only_ that error, not all possible errors. – Charles Duffy Jun 12 '22 at 23:28

1 Answers1

0

The only point of failure that I can see is the first input.

Try this:

I am assuming that knights, weapon, and armour are lists with a string as their first element. e.g. ["lancelot"],["axe"],["helmet"]

def update_data(knights, weapon, armour):
  print("What would you like to update ?")
  print("1: Knights name: " + knights[0])
  print("2: Weapon: " + weapon[0])
  print("3: Armour: " + armour[0])
  

  try:   
    selection = input("Select your Option: ")
    if selection.isdigit():
        selection = int(selection)
    else:
        print("Selection must be an integer, not: " + selection)
        raise ValueError
 
    if selection == 1:
        knights[0] = str(input("what is their new name ? " + knights[0]))
        print("Your Knights new name is: " + knights[0])
    elif selection == 2:
        weapon[0] = str(input("What is their new weapon ? " + weapon[0]))
        print("They are now armed with: " + weapon[0])
    elif selection == 3:
        armour[0] = str(input("What new armour shall they wear ? " + armour[0]))
        print("They are now wearing: " + armour[0])
  except:
    print("---Try Again---")
    update_data(knights, weapon, armour)
Alexander
  • 16,091
  • 5
  • 13
  • 29