【发布时间】:2021-12-31 08:04:04
【问题描述】:
我有一个程序,其中一个功能是出售用户拥有的物品。它提示用户输入名称(id)和金额,然后出售。但是用户可以拥有很多项目,所以有很多 if else elif 语句。我该如何缩短这个? (P.S.我用的是Replit,Replit目前有Python 3.8)这里是sell函数,供参考。
def sell_command():
global cash
cash = 0
#I created a dictionary, inventory, which has how much the user has of a particular item.
#itemSell variable contains what the user wants to sell
#itemSellCount variable contains how much the user wants to sell
#itemSoldCash variable calculates how much one item is worth, and multiplies for how much the user is selling
#cash variable is hlobal since another function prints cash
itemSell = input('What would you like to sell? ')
itemSell = itemSell.lower()
if itemSell == "cobblestone" or "cobble stone":
itemSellCount = int(input("How many would you like to sell? "))
if itemSellCount <= inventory["cobblestone"]:
itemSoldCash = itemSellCount*10
print("You sold " + str(itemSellCount) + " cobblestone/s for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["cobblestone"] -= itemSellCount
elif itemSellCount > inventory["cobblestone"]:
print("You tried to sell more than what you have!")
elif itemSell == "coal":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["coal"]:
itemSoldCash = itemSellCount*5
print("You sold " + str(itemSellCount) + " coal for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["coal"] -= itemSellCount
elif itemSellCount > inventory["coal"]:
print("You tried to sell more than what you have!")
elif itemSell == "iron ore" or "ironore":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["ironOre"]:
itemSoldCash = itemSellCount*20
print("You sold " + str(itemSellCount) + " iron ore/s for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["cobblestone"] -= itemSellCount
elif itemSellCount > inventory["ironOre"]:
print("You tried to sell more than what you have!")
elif itemSell == "iron ingot" or "ironingot":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["ironIngot"]:
itemSoldCash = itemSellCount*25
print("You sold " + str(itemSellCount) + " iron ingot/s for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["ironIngot"] -= itemSellCount
elif itemSellCount > inventory["ironIngot"]:
print("You tried to sell more than what you have!")
elif itemSell == "emerald" or "emeralds":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["emerald"]:
itemSoldCash = itemSellCount*100
print("You sold " + str(itemSellCount) + "emerald/s for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["emerald"] -= itemSellCount
elif itemSellCount > inventory["emerald"]:
print("You tried to sell more than what you have!")
elif itemSell == "diamond" or "diamonds":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["diamond"]:
itemSoldCash = itemSellCount*300
print("You sold " + str(itemSellCount) + " diamond/s for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["diamond"] -= itemSellCount
elif itemSellCount > inventory["diamond"]:
print("You tried to sell more than what you have!")
elif itemSell == "oak":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["oak"]:
itemSoldCash = itemSellCount*15
print("You sold " + str(itemSellCount) + " oak/s for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["oak"] -= itemSellCount
elif itemSellCount > inventory["oak"]:
print("You tried to sell more than what you have!")
elif itemSell == "birch":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["birch"]:
itemSoldCash = itemSellCount*15
print("You sold " + str(itemSellCount) + " birch for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["birch"] -= itemSellCount
elif itemSellCount > inventory["birch"]:
print("You tried to sell more than what you have!")
elif itemSell == "redwood" or "red wood":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["redwood"]:
itemSoldCash = itemSellCount*15
print("You sold " + str(itemSellCount) + "redwood for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["redwood"] -= itemSellCount
elif itemSellCount > inventory["redwood"]:
print("You tried to sell more than what you have!")
elif itemSell == "spruce":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["spruce"]:
itemSoldCash = itemSellCount*15
print("You sold " + str(itemSellCount) + " spruce for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["spruce"] -= itemSellCount
elif itemSellCount > inventory["spruce"]:
print("You tried to sell more than what you have!")
elif itemSell == "acacia":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["acacia"]:
itemSoldCash = itemSellCount*15
print("You sold " + str(itemSellCount) + " acacia for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["acacia"] -= itemSellCount
elif itemSellCount > inventory["acacia"]:
print("You tried to sell more than what you have!")
elif itemSell == "jungle":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["jungle"]:
itemSoldCash = itemSellCount*15
print("You sold " + str(itemSellCount) + " jungle for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["jungle"] -= itemSellCount
elif itemSellCount > inventory["jungle"]:
print("You tried to sell more than what you have!")
elif itemSell == "maple":
itemSellCount = int(input("How many would you like to sell?"))
if itemSellCount <= inventory["maple"]:
itemSoldCash = itemSellCount*15
print("You sold " + str(itemSellCount) + " maple for $" + str(itemSoldCash))
cash = cash + itemSoldCash
inventory["maple"] -= itemSellCount
elif itemSellCount > inventory["maple"]:
print("You tried to sell more than what you have!")
【问题讨论】:
-
首先,
itemSell == "cobblestone" or "cobble stone"始终为真,因此实际上没有运行以下 else 语句(因此,如果您想缩短代码并具有相同的行为,则可以将它们全部删除)。 .. 例如,您是否尝试过输入煤炭?除此之外,请将帖子缩减为minimal reproducible example -
第一个如果有错别字,在第二个条件下它总是正确的
-
使用字典和辅助函数来避免几乎相同的块
标签: python if-statement python-3.8