我想制作一个货币转换器,它将从雅虎获取当前汇率并将其乘以用户想要的金额。但我无法将它们倍增。你能帮我吗?
我总是得到: 回溯(最近一次调用最后一次):文件" C:\ Users \ Ioannis \ Desktop \ c.py",第17行,在realprice = float(" price [0]")
import urllib
import re
stocklist = ["eurusd"]
i=0
while i<len(stocklist):
url = "http://finance.yahoo.com/q?s=eurusd=X"
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="yfs_l10_eurusd=x">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print "the price of", stocklist[i],"is" ,price[0]
i+=1
realprice = float("price[0]")
print ("Currency Exchange")
ex = raw_input("A-Euro to Dollars, B-Dollars to Euro")
if ex == "A":
ptd = int(raw_input("How much would you like to convert: "))
f = ptd*price[0]
print("It is $",f)
if ex == "B":
ptd = float(raw_input("How much would you like to convert"))
f = ptd*0.7
f2 = round(f,2)
print ("It is $",f2)
&#13;
ValueError:无法将字符串转换为float:price [0]
答案 0 :(得分:1)
问题在于这一行:
realprice = float("price[0]")
您要做的是将带有非数字字符的字符串转换为浮点数,但这不起作用。
更准确地说,您正在将字 price[0]
转换为数字。
要解决此问题,请删除上面一行中的"
个字符。像这样:
realprice = float(price[0])
(期待编辑,因为你有一些错误)