This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
I wish to accept a number in float from the user and give back an answer multiplying that number with pi value = 3.14
heres my code:
print "Enter a no."
n = raw_input('enter here: ')
result = 1
def gen_pi(x):
result = x*3.14
return float(result)
gen_pi(n)
its giving me an error of not being able to multiply non int sequence with float. what does it mean? anything wrong with the code?
The result of raw_input is a str which means that n is a str and then in your function, x is a str which can't multiply a float.
you'll want to convert x into a float before multiplying by pi.
There are lots of places to do this, but I'd recommend doing it before passing to gen_pi:
gen_pi(float(n))
This way, gen_pi can always assume it's working with a float and that code can be simplified:
def gen_pi(x):
return x * 3.14
Also note that if you want more precision (and possibly a bit more code clarity), you can use math.pi:
import math
def gen_pi(x):
return x * math.pi
Related
This question already has answers here:
How to get numbers after decimal point?
(37 answers)
Closed 1 year ago.
I'm having a hard time doing this I have tried experimenting with (0:.2f)
I want the program to only print the decimal part
Example inputs and outputs
Inputs
100.56
455.345
89.5
Outputs
.56
.345
.5
Is there a way to do this?
you may try the code as below
input = 100.56
print(input % 1)
you could refer to the answer at How to get numbers after decimal point?
If you want to have only the original decimal part, you can do :
inputFloat = 25189456.1584566
decimalPart = '.' + str(inputFloat).split('.')[1]
print(decimalPart)
Something like this should work
def f(x, decimals=2):
r = str(round(x, decimals)) #round and convert to string
r = r.split('.')[-1] #split at the dot and keep the decimals
r = '.' + r #add the dot
return r
f(100.56789) #.57
[f(x) for x in [100.56, 455.345, 89.5]] #['.56', '.35', '.5']
The one-liner would be '.' + str(round(x, 2)).split('.')[-1] or f".{str(round(x, 2)).split('.')[-1]}" where x is the float you are interested in.
This question already has answers here:
How do you check whether a number is divisible by another number?
(9 answers)
Closed 6 years ago.
Wondering what is the most efficient way to check if an integer can or cannot be divided by another number (could be float) in Python 2.7. Or more general, what is the most efficient way to check if an integer can be divided by n (n could be a float number) in Python 2.7.
My pain point is, if I try to get x/n, it is always an integer.
Try
if x % n == 0 :
hope this helps !
Here:
x = 25
y = 2.5 # Or something
if not x % y: # Works with float too
print 'can divide'
else:
print 'cannot divide'
This question already has answers here:
Can't convert string number value to float
(3 answers)
Closed 7 years ago.
I seem to have a problem with my code for a simple calculator with equations already codded into it that rounds to decimal points. The problem i have is that I have a variable that has a number from an equation but it isn't rounding to the nearest 2 decimal points when it needs to. Here is an example code:
def main():
variable1 = input("Input number")
variable2 = input("Input number")
V1 = float(variable1)
V2 = float(variable2)
variablesq = V1*V1
equation = 20242*(V2/variablesq)
answer = equation
round(answer, 2) #This is where the problem is occurring
print Answer
I do believe my mistake is that I'm using a variable instead of a set number but say the answer is 15.2353 it won't round the number from the variable to 15.24
Just calling the function won't do anything. You need to assign the result to something.
answer = round(answer, 2)
You can simply do "{0:.2f}".format(answer) that way you don't need to assign it to a variable.
This question already has answers here:
How to print list item + integer/string using logging in Python
(2 answers)
Closed 7 years ago.
I am coding in Python, and have reached an error that I cannot seem to solve. Here's the part of the code that it affects.
import random
a = raw_input("Enter text")
b = random.randrange(1,101)
print (a+b)
When I try to run the code, I get the error "TypeError: cannot concatenate 'str' and 'int' objects"
I want to know how to print the result of a+b.
To answer to the question in the title, you can convert an integer into a string with str. But the print function already applies str to its argument, in order to be able to print it.
Here, your problem comes from the fact that a is a string while b is an integer. The + operator works on two strings, or two ints, but not a combination of this two types. If you have two strings, the + will mean concatenate. If you have two ints, the + will mean add. It then depends on the result you want to get.
You can convert a string to an integer by using int.
Try this code:
import random
a = int (raw_input ("Enter int "))
b = random.randrange (1, 101)
print a + b
This question already has answers here:
Integer square root in python
(14 answers)
Closed 9 years ago.
I am trying to get the square root of a really large number yet I get the error:
deltaSqrt = pow(delta,0.5)
OverflowError: long int too large to convert to float
In my case delta is equal to:
5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216
What should I do to get the square root of this number?
Use decimal:
import decimal
>>> d = decimal.Decimal('5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216')
>>> d.sqrt()
Decimal('7.139694782779097001143800270E+307')
If nothing else works, try this:
http://code.google.com/p/gmpy/