
Baker S. answered 02/07/23
Tutor
5
(2)
Stanford CS Major with 5+ years teaching and tutoring experience
You can call this function and it will prompt the user to enter a number! If they give something that's not a positive integer, it will ask again.
def get_num_items():
while True:
num_items = input("Enter the number of items you want to purchase: ")
if num_items.isdigit() and int(num_items) > 0:
return int(num_items)
else:
print("Invalid input. The number of items must be a positive integer.")