我试图制作一个简单的转换程序,但是输出结果要么很奇怪,要么根本没有。我该如何解决这个问题?这是我的密码
#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()
将其附加到crypto2Crypto函数的末尾
不确定w是否是您需要的,但返回您需要的
三个问题
=+
运算符(是,复数)与+=
运算符不同分配(
=
)为什么??因为
a =+ 1
变成了a = +1
⇒a = 1
扩充赋值(
+=
)为什么??因为
a += 1
变成了a = a + 1
⇒a = 2 + 1
⇒a = 3
。更多关于扩充赋值here如果您自己不从函数返回某些值,Python将自动使其返回
None
。因此,您应该向crypto2Crypto
添加一个return
语句。这已在下一节的解决方案中显示二进制浮点数(Python的
float
类型,在main
中用于获取amountOfCurrency
的值的类型)及其算术不准确。有关详细信息,请阅读Python教程的chapter 15解决方案
将
crypto2Crypto
函数更改为:至于浮点的奇怪之处,您可以使用内置的^{} 函数四舍五入到所需的小数位数
相关问题 更多 >
编程相关推荐