cmd trackback NameError: name '_' is not defined

cmd trackback NameError: name '_' is not defined

我有一个关于引用错误的快速问题

我的代码在 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 


只需在class Vending:前加上_ = "" 您收到此错误是因为您尚未在 class 中定义它。 我试过了,我得到了

————————————————
 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
==: