cmd trackback NameError:未定义名称“\u1”

2024-05-26 21:54:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于回溯错误的快速问题

我的代码在Jupyter中运行得很好,但是

我在CMD中运行代码时出错

我对一些定义使用虚拟变量

CMD似乎无法识别伪变量“\u1”

我得到的错误是:

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 



Tags: inselfformatreturnifdefchangetotal
1条回答
网友
1楼 · 发布于 2024-05-26 21:54:13

只需在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
==: 

相关问题 更多 >

    热门问题