我正在写一个货币转换器,其中一部分是一个程序,它使您可以检查和更改英镑与其他三种货币之间的汇率。虽然实际的转换很好,但是我在将新汇率导出到泡菜文件的一行中遇到问题。这是因为费率以以下格式写在泡菜文件中: 英镑1欧元1.15美元-1.3美元等
我将不胜感激。
我尝试仅转储一个列表和元组,但是它根本不起作用-这不是Python错误,而是一个逻辑错误。
import pickle
Currencies = "Pound-Sterling", "Euro", "US-Dollar", "Japanese-Yen"
Displayed = {}
Exchange_rates = pickle.load(open("rates.pkl","rb"))
print("This program allows you to check and change the currency conversion rates before using the converter program.")
for Line in Exchange_rates:
if not "Pound-Sterling" in Line:
Displayed_key = Line.split(" ")[0]
Displayed_value = Line.split(" ")[1]
Displayed[Displayed_key] = Displayed_value
print("£1 is", Line)
Exchange_rates = {}
New_rates = open("rates.pkl","wb")
pickle.dump("",New_rates)
pickle.dump("Pound-Sterling 1",New_rates)
if getYesNo("Do you want to change an exchange rate for a currency (Y/N):"):
for i in range(1,4):
Currency = Currencies[i]
print("£1 is", Currency, Displayed[Currency])
Prompt = "Please input the new rate for", Currency, ":"
Prompt = " ".join(Prompt)
New_rate = float(input(Prompt))
To_append = Currency, New_rate
pickle.dump(To_append,New_rates)
New_rates.close()
新的泡菜文件应以原始格式显示在一行上。相反,它要么行很多,要么不起作用,而且,我无法摆脱括号和逗号。
答案 0 :(得分:0)
这是您的代码版本,具有一些改进
改进:
printf("%f\n", 299792450.0);
。这称为上下文管理器,负责关闭文件。with open('Currencies.pkl', 'rb') as f:
print_currencies
更改为for i in range(1,4):
更具可读性,并且允许使用任何数量的货币。 for currency in CURRENCIES:
这里是字典,但可以是列表,也可以是任何可迭代的。需要改进的地方:
CURRENCIES