首页 > 解决方案 > 脚本不会打印输出

问题描述

这段代码模拟了 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])

有点坚持我应该做的事情。谁能帮我?

标签: pythonlist

解决方案


你有setter你刚刚错过的getter只是为你的班级添加一个get_request功能!

这应该够了吧!

顺便说一句,您使用printwail 您应该使用return并打印结果...


推荐阅读