无法将字符串转换为float:price[0]python

2024-09-30 03:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个货币转换器,它将采取当前的汇率从雅虎,乘以它的数额,用户想要。但我不能让它们成倍增长。你能帮帮我吗?在

我总是得到: 回溯(最近一次调用):文件“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)

ValueError:无法将字符串转换为float:price[0]

python code


Tags: toimportreinputrawisurllibfloat
1条回答
网友
1楼 · 发布于 2024-09-30 03:26:04

问题就在这条线上:

realprice = float("price[0]")

您要做的是将包含非数字字符的字符串转换为无效的浮点。在

更准确地说,您正在将单词price[0]转换为数字。在

要解决这个问题,只需删除上面一行中的"字符。像这样:

^{pr2}$

(由于有一些错误,请进行编辑)

相关问题 更多 >

    热门问题