[英]Using if statements with user inputs
我正在嘗試使用 python 創建一個簡單的產品庫存,但由於某種原因if
語句不會運行,我不知道出了什么問題。 下面是 output 和代碼輸入。
#代碼
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()
#輸出
What do you want to do?
Enter Add item, Quantity, Show Inventory:Add item
Process finished with exit code 0
嘗試這個:
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()
如果您想了解比較 python 中的字符串,請單擊此處
但你可以試試這段代碼:
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()
所以.lower()
會將任何大寫字母轉換為小寫字母,這樣可以減少錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.