cmd trackback NameError: имя '_' не определено - PullRequest
0 голосов
/ 27 мая 2020

У меня быстрый вопрос об ошибке обратного отслеживания

Мой код очень хорошо работает в Jupyter, но

У меня возникла ошибка при запуске кода в CMD.

Я использую фиктивную переменную для нескольких определений.

Кажется, что CMD не распознает фиктивную переменную '_'

Что у меня есть ошибка:

Traceback (most recent call last):

  File "Vending_Machine.py", line 86, in <module>

    print (' {} ({}) {} ({})'.format(drink[0], Vending(_, drink[0]).outofstock(), drink[1], 
Vending(_, drink[1]).outofstock()))

NameError: name '_' is not defined

Есть ли есть ли способ работать с этим кодом и в CMD?

Спасибо.

class Vending:
    def __init__(self, selection, soda): 
        self.__selection = selection 
        self.__soda = soda 
    def selection(self): 
        return self.__selection 
    
    def soda(self): 
        return self.__soda 
    
    def outofstock(self): 
        if stock[self.__soda] == 0: 
            return 'x' 
        else: # 
            return " " 

class Deposit(Vending): 
    Total = 0 
    Change = 0 
    def __init__ (self, selection, soda): 
        Vending.__init__(self, selection, soda) 
           
    def operating(self):
        try: 
            if self.selection() in Accept_coin: 
                Deposit.Total += int(self.selection())/100  
                if Deposit.Change != 0:
                    Deposit.Change = 0 
                    return Deposit.Change  
                return Deposit.Total 
            
            elif self.selection() not in Accept_coin: 
                if Deposit.Change != 0: # 
                    Deposit.Change = 0 #
                if self.selection() == 'Quit':
                    Breaking.append('Break')
                elif self.selection() not in drink: 
                    Deposit.Change += int(self.selection())/100
                    return Deposit.Change 
                
            if self.selection() in drink:
                if Deposit.Change != 0: 
                    Deposit.Change = 0 
                if Deposit.Total < 0.5: 
                    print('You do not have enough money to purchase it')
                if Deposit.Total >= 0.5: 
                    stock[self.selection()] -= 1 
                    Deposit.Change = Deposit.Total - 0.5 
                    Deposit.Total = 0 
        except ValueError:
            print('You put wrong input. Try again') 



Accept_coin = ['5', '10', '25'] 
drink = ['Coke', 'Jolt', 'Pepsi', 'Diet'] 
stock = {'Coke':2, 'Jolt':2, 'Pepsi':2, 'Diet':2} 
Breaking = [] 
while True: 
    print ('————————————————') 
    if 0 not in stock.values(): 
        print (' {} ( ) {} ( )'.format(drink[0], drink[1]))
        print (' {} ( ) {} ( )'.format(drink[2], drink[3]))
        print ('Total: {:,.2f}  Change: {:,.2f}'.format(Deposit.Total, Deposit.Change)) 
             
    else: 
        print (' {} ({}) {} ({})'.format(drink[0], Vending(_, drink[0]).outofstock(), drink[1], Vending(_, drink[1]).outofstock()))
       
        print (' {} ({}) {} ({})'.format(drink[2], Vending(_, drink[2]).outofstock(),drink[3],Vending(_, drink[3]).outofstock()))
   
        print ('Total: {:,.2f}  Change: {:,.2f}'.format(Deposit.Total, Deposit.Change)) 
    
    
    a = Deposit(input('==:').title(), _).operating() 
    if 'Break' in Breaking: 
        break 


1 Ответ

0 голосов

Просто добавьте _ = "" перед class Vending: Вы получаете эту ошибку, потому что вы не определили ее вне класса. Я попробовал и получил

————————————————
 Coke ( ) Jolt ( )
 Pepsi ( ) Diet ( )
Total: 0.00  Change: 0.00
==:Coke
You do not have enough money to purchase it
————————————————
 Coke ( ) Jolt ( )
 Pepsi ( ) Diet ( )
Total: 0.00  Change: 0.00
==: 
...