简体   繁体   English

如何在 python 中显示 function 的 output?

[英]How to display output of a function in python?

I`m trying to make a simple conversion program but the output keeps coming out either weird or none at all.我正在尝试制作一个简单的转换程序,但 output 总是出现奇怪或根本没有出现。 How do I fix this?我该如何解决? Here is my code.这是我的代码。

#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()

Three problems三个问题

  1. The =+ operators( yes, plural ) are not the same as the += operator. =+运算符(是的,复数)与+=运算符不同。

    • An assignment ( = )赋值 ( = )

       >>> a = 2 >>> a =+ 1 >>> a 1

      Why?为什么? Because a =+ 1 becomes a = +1a = 1 .因为a =+ 1变成a = +1a = 1

    • An augmented assignment ( += )一个增广的赋值( +=

       >>> a = 2 >>> a += 1 >>> a 3

      Why?为什么? Because a += 1 becomes a = a + 1a = 2 + 1a = 3 .因为a += 1变成a = a + 1a = 2 + 1a = 3 More on augmented assignments here .更多关于增强任务在这里

  2. If you don't return some value from a function yourself, Python will automatically make it return None .如果您自己没有从 function 返回某些值,则 Python 将自动使其返回None So, you should add a return statement to crypto2Crypto .因此,您应该向crypto2Crypto添加一个return语句。 This has been shown in the solution in the next section.这已在下一节的解决方案中显示。

  3. Binary floating-point numbers (Python's float type, the one you used in main to get the value for amountOfCurrency ) and their arithmetic are not accurate.二进制浮点数(Python 的float类型,您在main中用于获取amountOfCurrency的值)及其算术不准确。 Read The Python Tutorial's chapter 15 for details.有关详细信息,请阅读 Python 教程的第 15 章

Solution解决方案

Change the crypto2Crypto function to:crypto2Crypto function 更改为:

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

As for the floating-point weirdness, you can use the built-in round function to round-off to the required number of decimal places.至于浮点怪异,您可以使用内置的round function 舍入到所需的小数位数。

Append this to the end of crypto2Crypto function Append 这个到crypto2Crypto function的结尾

return w 

Not certain if w is what you need, but return what you need to不确定 w 是否是您需要的,但返回您需要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM