Alex
Alex

Reputation: 13

Issues multiplying in python

I'm trying to make a very simple BTC To USD converter in python (bear with me I'm in the beginning stages), however when I print the value it prints the entered number 275 times (the value of 1 btc). I need it to return the amount entered by the user times 275 (so the user enters 1 and it multiplies 1 by 275 to return 275) Here is my current code:

enteramt = raw_input ("How many")
value = enteramt * 275
print ("This is worth" , value)

What it prints: This is worth, 555555555555555555555555555555555555555555555555555555555555555

Except, theres 275 of them, but you get the point

Upvotes: 1

Views: 232

Answers (3)

Brushline
Brushline

Reputation: 47

You need to convert your raw_input()

So your new code should be like this:

enteramt = float(raw_input("How many"))
value = enteramt * 275
print ("This is worth" , value)

Or this:

enteramt = raw_input("How many")
value = float(enteramt) * 275
print ("This is worth" , value)

Upvotes: 0

igavriil
igavriil

Reputation: 1021

This is because enteramt is a string. Multiplying a string in python will produce the string repeated. For example:

str = '12'
str * 4
>>> '12121212'

You should convert your input to float or int:

str = float(str)
str * 4
>>> 48.0

Upvotes: 0

tdelaney
tdelaney

Reputation: 77407

In python 2 raw_input returns a string and in python somestring * value repeats the string value times. You want to convert the input to an int and then do the multiplication. (you also want to remove the parens from the print, that's a python 3 thing)

enteramt = raw_input ("How many")
value = int(enteramt) * 275
print "This is worth" , value

In python 3, raw_input is replaced by input, so you'd write

enteramt = input ("How many")
value = int(enteramt) * 275
print("This is worth" , value)

Upvotes: 2

Related Questions