python - 脚本不会打印输出
问题描述
这段代码模拟了 5 楼的电梯,人们进来并选择目的地楼层。代码工作正常,但是当我要求用户输入时,它不会打印输出。
像这儿,
class Elevator:
def __init__(self,initial= 5,floors=35,speed=10):
self.__initial_value = initial
self.__current_value = initial
self.__number_floors = floors
self.__speed = speed
def set_requests(self,floor_requests):
print("="*20 , 'New Request', "="*20)
error, valid = self.check_requests(floor_requests)
if len(error) > 0:
print("Wrong in the series of floor requests: ", error)
if len(valid) != 0:
self.__requests = valid
self.movement()
print()
def movement(self):
self.__requests.sort()
print('valid requests : ', self.__requests)
print('Starting ...\n')
for floor in self.__requests:
if floor != self.__current_value:
print("Current floor of the elevator: ", str(self.__current_value))
print("next destination of the elevator: ", str(floor))
step = abs(self.__current_value - floor)
print("the floors left to arrive at the destination: ", str(step))
print("time left to arrive at the destination: ", str(step*self.__speed) ,'second')
print("Exit the client on the floor: ",floor)
self.__current_value = floor
def check_requests(self,requests):
error = []
valid_requests = []
for request in requests:
if 0 > request or request > self.__number_floors-1:
error.append(request)
else:
valid_requests.append(request)
return error,valid_requests
e = Elevator()
e.set_requests = list()
clients = input("Enter the number of clients: ")
for i in range(int(clients)):
n = input("destinations: ")
e.set_requests.append(int(n))
但是,当我设置一些示例时,它可以正常工作:
class Elevator:
def __init__(self,initial= 5,floors=35,speed=10):
self.__initial_value = initial
self.__current_value = initial
self.__number_floors = floors
self.__speed = speed
def set_requests(self,floor_requests):
print("="*20 , 'New Request', "="*20)
error, valid = self.check_requests(floor_requests)
if len(error) > 0:
print("Wrong in the series of floor requests: ", error)
if len(valid) != 0:
self.__requests = valid
self.movement()
print()
def movement(self):
self.__requests.sort()
print('valid requests : ', self.__requests)
print('Starting ...\n')
for floor in self.__requests:
if floor != self.__current_value:
print("Current floor of the elevator: ", str(self.__current_value))
print("next destination of the elevator: ", str(floor))
step = abs(self.__current_value - floor)
print("the floors left to arrive at the destination: ", str(step))
print("time left to arrive at the destination: ", str(step*self.__speed) ,'second')
print("Exit the client on the floor: ",floor)
self.__current_value = floor
def check_requests(self,requests):
error = []
valid_requests = []
for request in requests:
if 0 > request or request > self.__number_floors-1:
error.append(request)
else:
valid_requests.append(request)
return error,valid_requests
e = Elevator()
e.set_requests([25])
e.set_requests([60])
e.set_requests([17])
有点坚持我应该做的事情。谁能帮我?
解决方案
你有setter
你刚刚错过的getter
只是为你的班级添加一个get_request
功能!
这应该够了吧!
顺便说一句,您使用print
wail 您应该使用return
并打印结果...
推荐阅读
- blogger - 通过没有 Javascript 的背景图像的 Blogger 特色图像
- windows - 默认 Windows 缩放到 150%?
- python - 为什么这个参数列表在 Python 中不会改变?
- amazon-web-services - 调用 ListTopicRules 操作时发生错误(UnrecognizedClientException):请求中包含的安全令牌无效
- jquery - Azure AD 身份验证 401 错误“受众无效” AddAzureADBearer .Net Core Web Api
- jquery - 循环遍历表时如何将多选值推送到Jquery数组
- npm - 从动态导入的 ES6 模块加载 CSS
- python - 如何在 TensorFlow 2.0 中计算二阶导数(Hessian 的对角线)
- android - 数组列表