def getStocks():
stockNames = []
stockPrices = []
done = 0
while done != 1:
stock = input('Enter Stock symbol: ')
if stock == 'done':
done = 1
else:
price = int(input('Enter Price of Stock: '))
print("")
stockNames.append(stock)
stockPrices.append(price)
return stockNames, stockPrices
问题是"输入股票代码:"即使在用户键入“完成”之后出现,如何在此时终止无限循环?我尝试使用break但它没有提供我正在寻找的结果
答案 0 :(得分:1)
而不是input
使用raw_input
它将解决问题
def getStocks():
stockNames = []
stockPrices = []
done = 0
while done != 1:
stock = raw_input('Enter Stock symbol: ')
if stock == 'done':
done = 1
else:
price = int(input('Enter Price of Stock: '))
print("")
stockNames.append(stock)
stockPrices.append(price)
return stockNames, stockPrices
python版本:2.7 +
答案 1 :(得分:0)
您可能想要raw_input()
,因为input()
实际上会尝试评估它返回的表达式。