I am trying to create a simple product inventory using python but for some reason the if
statement won't run and I don't know what's wrong. Below is the output and inputs of the code.
#Code
def inventory_products():
choice = str(input("What do you want to do? \nEnter Add item, Quantity, Show Inventory:"))
if choice == 'Add item':
input("Enter name")
inventory_products()
#Output
What do you want to do?
Enter Add item, Quantity, Show Inventory:Add item
Process finished with exit code 0
Try this:
def inventory_products():
choice = str(input("What do you want to do? \nEnter Add item, Quantity, Show Inventory:"))
if choice.strip() == 'Add item': # change in code to remove extra spaces
input("Enter name")
inventory_products()
If you want to know about comparing strings in python click here
but you can try this code:
def inventory_products():
choice = str(input("What do you want to do? \nEnter Add item, Quantity, Show Inventory:")).lower()
if choice.strip() == 'add item':
input("Enter name: ")
else:
print("Invalid Choice!")
inventory_products()
So .lower()
will convert any capital letter to small letters, so that errors can be reduced
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.