我有一个关于 Trackback 错误的快速问题
我的代码在 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