【发布时间】:2016-01-09 18:22:17
【问题描述】:
首先。我的代码:
UserInput = ("null") #Changes later
def ask_module(param, param2):
elif UserInput == (param):
print(param2)
while True:
UserInput = input()
UserInput = UserInput.lower()
print()
if UserInput == ("test"):
print("test indeed")
ask_module("test2", "test 2")
我不擅长编码,所以这可能是我做错了
这篇文章似乎有点公国,因为我几乎只有代码, 但我完全不知道如何完成这项工作。
没有缩短的代码是什么样子的:
while True:
UserInput = input()
UserInput = UserInput.lower()
print()
if UserInput == ("inventory"):
print("You have %s bobby pin/s" %bobby_pin)
print("You have %s screwdriver/s" %screwdriver)
elif UserInput == ("look at sink"):
print("The sink is old, dirty and rusty. Its pipe has a bobby pin connected")
else:
print("Did not understand that")
编辑:我发现可能很难看出我在问什么。
我想知道如何缩短我的原始代码
【问题讨论】:
-
做什么工作?你的程序发生了什么?此外,缩进是 Python 中的 KEY。
-
好的,我做了一个文字冒险,但很快就变成了重复,我想缩短重复。我将在我的原始代码中进行编辑。
-
if、elif和else在函数中的工作方式与在函数外的工作方式相同。def f(x): if x > 10 print "Big" else print "Small" -
我在原始代码中进行了编辑。
标签: python function python-3.x python-3.5 definitions