Sometimes your iPhone or iPad can shut down unexpectedly when there is battery left according to the battery percentage meter or the battery percentage jumps up or down. This usually means that the device battery needs recalibrating. Do like this:
Let the device battery drain completely until it shuts off. Streaming movies with maximum screen brightness is one of the faster methods.
Recharge the iPhone or iPad fully.
Force restart the device by holding home button and power on/off until you see the Apple logo. For iPhone 7 you hold down power on/off and volume down until the Apple logo shows.
Repeat steps 1 to 3 once.
Hopefully this resolves any strange battery behavior on your iPhone or iPad. If your device still behaves strangely after you recalibrate the battery you probably need to visit an Apple retailer.