這裏是我的代碼,它不斷出現相同的錯誤代碼,我不知道爲什麼我得到的錯誤或如何與我的代碼相關的任何信息將幫助!我不是很善於與錯誤的工作在我的代碼幫助錯誤請python
import urllib.request
import codecs
import matplotlib.pyplot as pyplot
import copy
from time import gmtime, strftime
from urllib.request import urlopen
def readInfo(filename):
myFile = open(filename,'r')
data=myFile.readlines()
namelist=[i.split(' ', 1)[0] for i in data]
balance = list()
for item in data:
name = item.split()[0]
amount = float(item.split()[1])
balance.append([name, amount])
print("n",namelist,"c",balance)
return (namelist, balance)
def fetch(url):
def find_element(line, s_pattern, e_pattern, position=0):
shift = len(s_pattern)
start = line.find(s_pattern, position) + shift
position = start
end = line.find(e_pattern, position)
return (line[start:end], position)
html = urlopen(url)
records = []
i = 0
for line in html.readlines():
line = line.decode()
if "<tr><td>" not in line:
continue # skip if line don't contain rows
if "Currency" in line:
continue # skip header
start = "<tr><td>"
end = "</td>"
element, start_pos = find_element(line, start, end)
records.append([element])
start = "<td>"
values = []
for x in range(2):
element, start_pos = find_element(line, start, end, start_pos)
values.append(element)
records[i].append(values)
i = i + 1
print(records)
return(records)
def findCurrencyValue(records, currency_name):
d = dict(records)
print(d[currency_name])
return(d[currency_name])
def transaction(filename, namelist, orgBalance, url):
exchange_info= fetch(url)
#Read each line from transactions.txt
myFile = open(filename,'r')
data=myFile.readlines()
#Check which company is conducting transactions
bank = dict(orgBalance)
for line in data:
company,action,currency,ammount = line.split()
do_something(company,action,currency,ammount,bank)
#If BUY, then convert the amount of foreign currency to USD
#and subtract the calculated amount
def find_currency_rate(currency):
# locate the curency name in the text body and find the last <td></td> value in that row...
return float(last_td_cell_of_row)
def convert_to_usd(currency,amount):
currency_rate = find_currency_rate(currency)
return amount*currency_rate
def do_something(company_name,action,currency_code,amount,bank):
amount_in_usd = convert_to_usd(currency_code,amount)
if action == "BUY":
bank[company_name] = bank[company_name] - amount_in_usd
else: # else we sell and add the funds to our bank
bank[company_name] = bank[company_name] + amount_in_usd
def main():
#get the namelist and original balance for all companies
filename1 = "balance.txt"
namelist, orgBalance = readInfo(filename1)
#specifies the URL
url = "https://www.cs.purdue.edu/homes/jind/exchangerate.html"
#calculate the current balance for all companies
balance = copy.deepcopy(orgBalance)
filename2 = "transactions.txt"
curBalance = transaction(filename2, namelist, balance, url)
#output the value for the original balance for each company
#this output should be a list of lists
print("Original Balance of each company is: ", orgBalance)
#output the value for the current balance for each company
#this output should be a list of lists
print("Current Balance of each company is: ", curBalance)
#call your bar graph plotting function
plotBarGraph(orgBalance, curBalance)
#call your pie graph plotting function
plotPieChart(curBalance)
main()
當我運行的代碼,我碰到下面的錯誤,我需要幫助固定
Traceback (most recent call last):
File "C:\Users\noahd\Desktop\project3\project3-skeleton.py", line 134, in <module>
main()
File "C:\Users\noahd\Desktop\project3\project3-skeleton.py", line 123, in main
curBalance = transaction(filename2, namelist, balance, url)
File "C:\Users\noahd\Desktop\project3\project3-skeleton.py", line 63, in transaction
company,action,currency,ammount = line.split()
ValueError: too many values to unpack (expected 4)
錯誤是相當清楚的告訴你,看你'line'變量,因爲它在拆分時並不完全包含4件事情。打印出來,看看有什麼。 – BlivetWidget
使用類似[pycharm](https://www.jetbrains.com/pycharm/download/)的調試器來遍歷代碼,或者在調試器中運行,並且在引發異常時它應該會自動中斷 –
我知道在哪裏錯誤是,我只是想看看是否有人可以幫助我跟蹤這個錯誤的實際問題,如果有人可以幫我修復它 –