像这样的案例有什么捷径吗?
Are there any shortcuts for a case like this?
大家好!我是编程新手,我一直无法找到这个问题的正确答案。我会尽力解释它:
我想知道是否有任何方法可以缩短我的代码,以免在下面的情况下变得如此重复。
class vehicle:
colour = 0
turbo = 0
quality = 1
type = 0
def create_vehicle(self, pick_colour, add_turbo, define_quality, pick_type):
self.colour = pick_colour
self.turbo = add_turbo
self.quality = define_quality
self.type = pick_type
return (self.colour, self.turbo, self.quality, self.type)
#This is the part I want to shorten up#
def check_vehicle(self):
if self.type == 0:
self.type = "motorbike"
elif self.type == 1:
self.type = "car"
elif self.type == 2:
self.type = "van"
if self.quality == 1:
self.quality = "basic"
elif self.quality == 2:
self.quality = "normal"
elif self.quality == 3:
self.quality = "good"
elif self.quality == 4:
self.quality = "superior"
if self.colour == 0:
self.colour = "white"
elif self.colour == 1:
self.colour = "red"
elif self.colour == 2:
self.colour = "yellow"
elif self.colour == 3:
self.colour = "blue"
elif self.colour == 4:
self.colour = "green"
elif self.colour == 5:
self.colour = "black"
elif self.colour == 6:
self.colour = "orange"
elif self.colour == 7:
self.colour = "grey"
if self.turbo == 0:
self.turbo = "does not have"
elif self.turbo == 1:
self.turbo = "has"
print("The %s I've created has a %s quality. It is %s and %s turbo" % (self.type, self.quality, self.colour, self.turbo))
"""
Types:
0 = Motorbike
1 = Car
2 = Van
Quality:
From 1 to 4
Increases general stats of the vehicle (Speed, Appearance, Maneuverability)
1 = Basic
2 = Normal
3 = Good
4 = Superior
Colour:
0 = White
1 = Red
2 = Yellow
3 = Blue
4 = Green
5 = Black
6 = Orange
7 = Grey
"""
vehicle1 = vehicle()
vehicle1.create_vehicle(5, 1, 4, 0)
vehicle1.check_vehicle()
正如我自己检查的那样,此代码输出以下内容:
The motorbike I've created has a superior quality. It is black and has turbo
有效!耶!
但是,问题是我想缩短上面突出显示的部分代码。感谢您的帮助
如果您使用的是 Python 2.7,您需要继承 Object。即
class vehicle:
应该变成
class vehicle(Object):
有关原因的信息,请参阅 Python class inherits object
根据您的用例,定义构造函数(又名 init)可能比 create_vehicle(..).
更好
有关详细信息,请参阅 https://micropyramid.com/blog/understand-self-and-init-method-in-python-class/
对于check_vehicle,我会使用字典进行简化。例如
types = {
1: "motorbike",
2: "car",
3:......etc
}
然后在
def check_vehicle(self):
if self.type in types:
self.type = types[self.type]
else:
print "My error message"
您可以使用列表或字典将代码转换为描述。但是,如果您只想打印描述,我建议您避免更新对象的成员。
def check_vehicle(self):
types = [ "motorbike", "car", "van" ]
qualities = [ "", "basic", "normal", "good", "superior" ]
colours = [ "white", "red", "yellow", "blue", "green", "black" ]
hasTurbo = ["does not have","has"]
self.type = types[self.type]
self.quality = qualities[self.quality]
self.colour = colours[self.colour]
self.turbo = hasTurbo[self.turbo]
print("The %s I've created has a %s quality. It is %s and %s turbo" % (self.type, self.quality, self.colour, self.turbo))
大家好!我是编程新手,我一直无法找到这个问题的正确答案。我会尽力解释它:
我想知道是否有任何方法可以缩短我的代码,以免在下面的情况下变得如此重复。
class vehicle:
colour = 0
turbo = 0
quality = 1
type = 0
def create_vehicle(self, pick_colour, add_turbo, define_quality, pick_type):
self.colour = pick_colour
self.turbo = add_turbo
self.quality = define_quality
self.type = pick_type
return (self.colour, self.turbo, self.quality, self.type)
#This is the part I want to shorten up#
def check_vehicle(self):
if self.type == 0:
self.type = "motorbike"
elif self.type == 1:
self.type = "car"
elif self.type == 2:
self.type = "van"
if self.quality == 1:
self.quality = "basic"
elif self.quality == 2:
self.quality = "normal"
elif self.quality == 3:
self.quality = "good"
elif self.quality == 4:
self.quality = "superior"
if self.colour == 0:
self.colour = "white"
elif self.colour == 1:
self.colour = "red"
elif self.colour == 2:
self.colour = "yellow"
elif self.colour == 3:
self.colour = "blue"
elif self.colour == 4:
self.colour = "green"
elif self.colour == 5:
self.colour = "black"
elif self.colour == 6:
self.colour = "orange"
elif self.colour == 7:
self.colour = "grey"
if self.turbo == 0:
self.turbo = "does not have"
elif self.turbo == 1:
self.turbo = "has"
print("The %s I've created has a %s quality. It is %s and %s turbo" % (self.type, self.quality, self.colour, self.turbo))
"""
Types:
0 = Motorbike
1 = Car
2 = Van
Quality:
From 1 to 4
Increases general stats of the vehicle (Speed, Appearance, Maneuverability)
1 = Basic
2 = Normal
3 = Good
4 = Superior
Colour:
0 = White
1 = Red
2 = Yellow
3 = Blue
4 = Green
5 = Black
6 = Orange
7 = Grey
"""
vehicle1 = vehicle()
vehicle1.create_vehicle(5, 1, 4, 0)
vehicle1.check_vehicle()
正如我自己检查的那样,此代码输出以下内容:
The motorbike I've created has a superior quality. It is black and has turbo
有效!耶! 但是,问题是我想缩短上面突出显示的部分代码。感谢您的帮助
如果您使用的是 Python 2.7,您需要继承 Object。即
class vehicle:
应该变成
class vehicle(Object):
有关原因的信息,请参阅 Python class inherits object
根据您的用例,定义构造函数(又名 init)可能比 create_vehicle(..).
更好有关详细信息,请参阅 https://micropyramid.com/blog/understand-self-and-init-method-in-python-class/
对于check_vehicle,我会使用字典进行简化。例如
types = {
1: "motorbike",
2: "car",
3:......etc
}
然后在
def check_vehicle(self):
if self.type in types:
self.type = types[self.type]
else:
print "My error message"
您可以使用列表或字典将代码转换为描述。但是,如果您只想打印描述,我建议您避免更新对象的成员。
def check_vehicle(self):
types = [ "motorbike", "car", "van" ]
qualities = [ "", "basic", "normal", "good", "superior" ]
colours = [ "white", "red", "yellow", "blue", "green", "black" ]
hasTurbo = ["does not have","has"]
self.type = types[self.type]
self.quality = qualities[self.quality]
self.colour = colours[self.colour]
self.turbo = hasTurbo[self.turbo]
print("The %s I've created has a %s quality. It is %s and %s turbo" % (self.type, self.quality, self.colour, self.turbo))