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)