Уведомления

Группа в Telegram: @pythonsu

#1 Ноя. 11, 2015 14:14:05

Unison
Зарегистрирован: 2015-10-28
Сообщения: 3
Репутация: +  0  -
Профиль   Отправить e-mail  

Задача № 3

Помогите написать код для автомата который продает Колу и Энергетик
Условия такие:
Кола стоит 1,5 Евро (reqCoka)
Энергетик 2 евро. (reqEnergy)
Напитки можно выбирать только когда уже внес деньги
Автомат принимает только 50 центов (inp50), 1 Евро (inp100 ) и 2 Евро (inp200)
Сдачу не выдает
После 2 евро деньги больше не принимает.

Вот такую подсказку нам оставили:
def next_state(state, inp):
“”“Calculate the successor state for a given state and input.

Args:
state (int): a natural number, representing the automaton's current state
inp (string): a string representing the automaton's input

Returns:
int: a natural number, representing the automaton's successor state

”“”
pass # TODO: replace by your implementation


def output(state):
“”“Calculate the output in a given state.

Args:
state (int): a natural number, representing the automaton's current state

Returns:
string: the output of the automaton

”“”
pass # TODO: replace by your implementation


def automaton():
“”“Main loop of the automaton.”“”
state = 0
while True:
state = next_state(state, input('> ‘))
print(output(state))


if __name__ == ’__main__':
automaton()


Надо, чтобы на выходе было так :

> inp50
50ct
> inp100
150ct
> inp100
250ct
> inp50
250ct
> reqCoke
COKE
> inp50
150ct


Спасибо

Отредактировано Unison (Ноя. 11, 2015 15:10:08)

Офлайн

#2 Ноя. 11, 2015 20:01:37

old_monty
Зарегистрирован: 2015-09-27
Сообщения: 238
Репутация: +  20  -
Профиль   Отправить e-mail  

Задача № 3

Предлагаю такое решение:

def automaton():
    """Main loop of the automaton"""
    money = 0
    state = 0
    def next_state(state, inp):
        """
        Calculate the successor state for a given state and input.
        Args:
        state (int): a natural number, representing the automaton's current state
        inp (string): a string representing the automaton's input
        Returns:
        int: a natural number, representing the automaton's successor state
        """
        d = {'inp50':50, 'inp100':100, 'inp200':200}
        nonlocal money
        if inp[:3] == 'inp' and money < 200:
            money += d[inp]
            return 0
        elif inp[:3] == 'inp' and money > 200:
            return 0
        if money >= 150 and inp == 'reqCoke':
            money -=150
            return 1
        if money >= 200 and inp == 'reqEner':
            money -= 200
            return 2
    def output(state):
        """
        Calculate the output in a given state.
        Args:
        state (int): a natural number, representing the automaton's current state
        Returns:
        string: the output of the automaton
        """
        if state == 0:
            return str(money) + 'ct'
        elif state == 1:
            return 'COKE'
        elif state == 2:
            return 'ENERGETIC'
 
    while True:
        state = next_state(state, input('> '))
        print(output(state))
if __name__ == '__main__':
     automaton()

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version