Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用函数获取用户输入_Python_Function_Input - Fatal编程技术网

Python 使用函数获取用户输入

Python 使用函数获取用户输入,python,function,input,Python,Function,Input,这是我的密码: def bitcoin_to_usd(btc): amount = btc * 527 print(amount) btc = input("Input your Bitcoins amount: ") bitcoin_to_usd(btc) 我想从用户那里得到比特币号码,然后我想计算它是多少美元 那个代码让我重复输入。例如,如果您输入2,它将返回22222222。。。。不算 我的Python版本是3.4.1,我正在使用PyCharm 有什么想法吗?在pyt

这是我的密码:

def bitcoin_to_usd(btc):
    amount = btc * 527
    print(amount)

btc = input("Input your Bitcoins amount: ")

bitcoin_to_usd(btc)
我想从用户那里得到比特币号码,然后我想计算它是多少美元

那个代码让我重复输入。例如,如果您输入2,它将返回22222222。。。。不算

我的Python版本是3.4.1,我正在使用PyCharm

有什么想法吗?

在python3.x中,input返回的是string1,而不是数字。如果您想要一个数字,您应该将输入字符串转换为浮点或整数

btc = float(input("Input your Bitcoins amount: "))

1这也解释了结果,将一个字符串乘以一个整数会导致该字符串与自身连接多次。

您的代码很好,只是需要将返回字符串的输入结果转换为数字。让我们尝试浮点数据类型的浮点:

def bitcoin_to_usd(btc):
    amount = btc * 527
    print(amount)

btc = float( input("Input your Bitcoins amount: ") )

bitcoin_to_usd(btc)

在定义的函数中使用以下行:

amount = float(btc) * 527
你可以用

btc = input("Input your Bitcoins amount: ")

def bitcoin_to_usd(btc):
    amount = btc * 527
    print(amount)


bitcoin_to_usd(btc)