python - 如何在python中显示函数的输出?
问题描述
我正在尝试制作一个简单的转换程序,但输出总是很奇怪或根本没有。我该如何解决?这是我的代码。
#crypto = [Bitcoin, Ethereum, XRP, Litecoin]
bitcoin = [1, 40.19, 38284.22, 168.73]
ethereum = [.025, 1, 951.99, 4.20]
xrp = [.000026, .001, 1, .003]
litecoin = [.0058, .231, 223.81, 1]
def crypto2Crypto(x,y,w):
if(x == "BE"):
w =+ (y * bitcoin[1])
if(x == "XL"):
y * xrp[3]
if(x == "EB"):
y * ethereum[0]
if(x == "LX"):
y * litecoin[2]
def main():
print("Welcome to the Cryptocurrency exchange!")
conversion = input('"What will you be converting today? B = Bitcoin, E = Ethereum, X = XRP, Litecoin = L. Please give an exchange with the following syntax crypto1crypto2, ex. type "BE" for Bitcoin to Ethereum."')
amountOfCurrency = float(input("How much do you have of " + conversion[0] + " ?"))
w = crypto2Crypto(conversion,amountOfCurrency,0)
print(w)
main()
解决方案
三个问题
=+
运算符(是的,复数)与+=
运算符不同。一个作业 (
=
)>>> a = 2 >>> a =+ 1 >>> a 1
为什么?因为
a =+ 1
变成a = +1
⇒a = 1
。增广赋值 (
+=
)>>> a = 2 >>> a += 1 >>> a 3
为什么?因为
a += 1
变成a = a + 1
⇒a = 2 + 1
⇒a = 3
。更多关于增强任务在这里。
如果你自己没有从函数中返回一些值,Python 会自动让它返回
None
。所以,你应该return
在crypto2Crypto
. 这已在下一节的解决方案中显示。二进制浮点数(Python 的
float
类型,用于main
获取 的值amountOfCurrency
)及其算术不准确。有关详细信息,请阅读 Python 教程的第 15 章。
解决方案
将函数更改crypto2Crypto
为:
def crypto2Crypto(x, y, w):
if x == "BE":
w += (y * bitcoin[1])
if x == "XL":
w += (y * xrp[3])
if x == "EB":
w += (y * ethereum[0])
if x == "LX":
w += (y * litecoin[2])
return w
至于浮点怪异,您可以使用内置round
函数四舍五入到所需的小数位数。
推荐阅读
- php - 我如何更改在 mysql 数据库上自动创建的表中的列名,而无需在每次需要完成时创建 ALTER TABLE 脚本
- python - 提取推文提及,包括“@”但不包括域部分,例如使用正则表达式的@xyz.com
- spring-boot - Springboot:如果 url 中有 %2F,则捕获异常
- django - Django - 第一次调用反转很慢
- c++ - 如何使用热键在 DEV C++ 命令提示符中粘贴文本?
- android - 使用 MVP 控制 Android 中的可见性
- java - 使用 GSON 解析器迭代数据集
- java - 带有 Firebase 搜索的 RecyclerView 在搜索时显示所有产品
- asp.net - Azure 构建管道:dotnet 构建失败,并出现:FileNotFoundException:无法加载文件或程序集 'TechTalk.SpecFlow,版本 = 3.1.0.0
- arcgis - 使用 JS API 隐藏 ArcGIS 地图上的重叠标记