Using the try-except statement, the fix is as follows:
while True:
try:
boxes = int(input("How many boxes do you have?"))
sweaters = int(input("How many sweaters do you have?"))
break
except:
print("Input invalid")
sweatersPerBox = sweaters/boxes
print("You need to put",sweatersPerBox,"sweaters in each box")
<h3>How to prevent the program from crashing?</h3>
The program is given as:
boxes = int(input("How many boxes do you have?"))
sweaters = int(input("How many sweaters do you have?"))
sweatersPerBox = sweaters/boxes
print("You need to put",sweatersPerBox,"sweaters in each box")
The above program would crash when the user enters string inputs.
Examples of string inputs are "7h", "bh"...
One way to fix the error is the use of the try-except statement.
Using the try-except statement, the fix is as follows:
while True:
try:
boxes = int(input("How many boxes do you have?"))
sweaters = int(input("How many sweaters do you have?"))
break
except:
print("Input invalid")
sweatersPerBox = sweaters/boxes
print("You need to put",sweatersPerBox,"sweaters in each box")
The above program would continue running until the user enters a valid input
Another way is to end the program when the user enters string inputs
Read more about python programs at:
brainly.com/question/26497128
#SPJ1