【发布时间】:2021-04-04 01:13:50
【问题描述】:
首先,因为我使用的代码很大,所以here 是代码的链接。
如果给出特定输入,我有这个函数可以运行其他函数:
allowed_commands = ['help', 'joblist', 'job', 'jobresign', 'work', 'bal', 'balance', 'dep', 'deposit', 'with', 'withdraw', 'fish', 'hunt', 'shop', 'buy', 'sell', 'hunt', 'fish', 'multiply']
def gamePlay():
while True:
command = input(f"{bright_green}Command (type [help] to see list of all commands):\n>> ")
while command in allowed_commands:
# <-- Display Rules -->
if command == 'help':
rules()
break
# <-- Display Jobs -->
elif command == 'joblist':
joblist_function()
break
# <-- Get Jobs -->
elif command == 'job' and working == False:
job_funtion()
break
elif command == 'job' and working == True:
print(f"\n{red}You are already doing a job. You can't work on two jobs,that is dumb...\n")
break
# <-- Resign Job -->
elif command == 'jobresign':
job_resign()
break
# <-- Work -->
elif command == 'work' and working == True:
work()
break
elif command == "work" and working == False:
print(f"{red}\nLOL, you don't have a job, how you gonna work?\n")
break
# <-- Deposit -->
elif command == 'dep' or command == 'deposit' and deposit_allowed != deposited:
dep_money()
break
elif command == 'dep' or command == 'deposit' and deposit_allowed == deposited:
print("You have a full bank kiddo...")
break
# <-- Balance -->
elif command == 'bal' or command == 'balance':
display_balance()
break
# <-- Withdraw -->
elif command == 'with' or command == 'withdraw' and deposited != 0:
withdraw_money()
break
elif command == 'with' or command == 'withdraw' and deposited == 0:
print(f"{red}\nNo money deposited. What are you even trying to wothdraw LOL?\n")
break
elif command == 'shop':
shop()
break
elif command == 'beg':
beg()
break
def beg():
global money
random_number2 = random.choice([0, 1, 2])
random_money = random.choice(range(100, 500))
if random_number2 == 1:
print("Ewwww beggar. No stonks for u")
if random_number2 == 2:
print(f"Mr.beggar, you can have ⏣ {random_money}.")
money += random_money
但函数下方绿线上的工具提示显示“圈复杂度太高:17(阈值 15)”。
通常,即使复杂度高达 30,这也适用于我的代码。但最后一个 elif 之后的代码不起作用。即使我输入'beg',该函数也不会运行:
Command (type [help] to see list of all commands):
>> beg
Command (type [help] to see list of all commands):
>> beg
Command (type [help] to see list of all commands):
>>
为什么会发生这种情况,我该如何解决?
【问题讨论】:
-
请重读How to create a Minimal, Reproducible Example。我们至少需要
allowed_commands和beg()来提供帮助。考虑将这个大的if-else块转换为使用dict,以便决定执行哪个函数。它对您的圈复杂度警告没有帮助,但它是 Python 中的标准做法,在许多其他语言中将是switch块。 -
在这种情况下我将如何使用 dict。我将在没有函数和列表的情况下重新编辑它
-
这个错误我不熟悉,但我认为用“if”替换“elif”可以解决问题。无论如何,你打破了循环,所以替换不会改变你的代码行为。
-
@GenisBillionaire,我认为您不明白:IDE 中的工具提示警告与代码的执行方式无关。这里也不例外,还有其他事情正在发生,我们需要查看其余引用的代码以提供帮助。
dict不会解决这个问题。 -
好的,我会添加需要的东西
标签: python function if-statement cyclomatic-complexity