Here is my code:
def bitcoin_to_usd(btc):
amount = btc * 527
print(amount)
btc = input("Input your Bitcoins amount: ")
bitcoin_to_usd(btc)
I want to get Bitcoin number from user then I want to calculate how much USD is it.
That code gives me repetition of the input. Such as if you input 2 it returns 222222222222222222222222.... doesn't calculate it.
My Python version is 3.4.1 and I am using PyCharm.
Any ideas?
5 Answers 5
Your code is fine except that you need to convert the result of input, which returns a string, to a number. Let's try float for a floating-point datatype:
def bitcoin_to_usd(btc):
amount = btc * 527
print(amount)
btc = float( input("Input your Bitcoins amount: ") )
bitcoin_to_usd(btc)
Comments
In python3.x, input returns a string1, not a number. If you want a number, you should convert the input string to a float or int.
btc = float(input("Input your Bitcoins amount: "))
1This explains the results as well, multiplying a string by an integer causes the string to be concatenated with itself that number of times.
Comments
You could use
btc = input("Input your Bitcoins amount: ")
def bitcoin_to_usd(btc):
amount = btc * 527
print(amount)
bitcoin_to_usd(btc)
Comments
you can try this where the function has no parameter :
def sum():
return x+y
x = int(input("Val of x"))
y = int(input("Val of y"))
print(sum())
or you can also try this one where the function has the parameter:
def sum(x,y):
return x+y
x = int(input("Val of x"))
y = int(input("Val of y"))
print(sum(x,y))
Comments
Use the below line inside your defined function:
amount = float(btc) * 527