Home > Software engineering >  My code still won't work if I input multiple attachments in python
My code still won't work if I input multiple attachments in python

Time:03-17

My code is now working but only if I input 1 value but if I input 2 values, it won't work. Example (I will input monolithic_suppressor;owc_skeleton_stock) the compiler wont read it. I want to input multiple attachments that add or subtract the following attributes on the man o war stats.

damage = 49
fire_rate = 50
accuracy = 69
mobility = 59
shooting_range = 56
controls = 53

attachments = "monolithic_suppressor;owc_skeleton_stock;owc_laser_tactical;operator_foregrip".split(";")
attachment = str(input("What attachment would you like to add? ")).lower()

while True:
        if attachment not in attachments:
            break
        elif attachment == attachments[0]:
             damage  = 5  
             mobility -= 5
        elif attachment == attachments[1]:
             mobility  = 5 
             accuracy -= 8
        elif attachment == attachments[2]:
             accuracy  = 5 
             controls  = 5
        elif attachment == attachments[3]:
             controls  = 10 
             mobility  = 2
        print(f"""\n Man O War Updated Stats!\n Damage: {damage}\n Fire Rate: {fire_rate}\n Accuracy: {accuracy} \n Mobility: {mobility}\n Range: {shooting_range} \n Control: {controls}
        """)
        break

CodePudding user response:

You need to use the split() method to attachments variable instead.

attachments = "monolithic_suppressor;owc_skeleton_stock;owc_laser_tactical;operator_foregrip".split(";")
attachment = str(input("What attachment would you like to add? ")).lower() 

Also, your if condition needs to be like this

if attachment == attachments[0]:
    damage  = 5
    mobility-= 5

CodePudding user response:

Always ask for input outside the loop. Keep asking with continue in case of errors. Check with if var == something of if var in something.

damage = 49
fire_rate = 50
accuracy = 69
mobility = 59
shooting_range = 56
controls = 53

attachments = "monolithic_suppressor;owc_skeleton_stock;owc_laser_tactical;operator_foregrip".split(";")
print(f"Options are {attachments}")
attachment = (input("What attachment would you like to add? ")).lower()


while True:
        if attachment not in attachments:
            attachment = (input("Incorrect Option.What attachment would you like to add? ")).lower()
            continue
        elif attachment in attachments[0]:
             damage  = 5  
             mobility -= 5
        elif attachment in attachments[1]:
             mobility  = 5 
             accuracy -= 8
        elif attachment in attachments[2]:
             accuracy  = 5 
             controls  = 5
        elif attachment in attachments[3]:
             controls  = 10 
             mobility  = 2
        print(f"""\n Damage: {damage}\n Fire Rate: {fire_rate}\n Accuracy: {accuracy} \n Mobility: {mobility}\n Range: {shooting_range} \n Control: {controls}
        """)
        break
  • Related