I'm trying to get this to work. The error I am getting is that I can't multiply sequence by non-int type 'float'.
import math
radius = input("hva er radiusen? ")
høyde = input("hva er høyden? ")
mOmkrets = (radius * math.tau)
Areal_sirkel = (math.pi * radius**2)
print("omkrets av høyden = ", (math.tau * {radius}))
print("Areal av sirkelen = ", math.pi * {radius}**2)
print("Areal av sylinderen = ", math.tau * {radius} * {høyde} 2 * {Areal_sirkel})
CodePudding user response:
By default, input()
returns string. But, we can't use a string to arithmetic operations like addition, multiplication, etc. Hence, place holder variables {}
is just used for complex variable substitutions and value formatting.
For example,
name = "James"
age = 20
print("my name is {name} my age is {age}".format(name=name, age=age))
Inorder to handle this problem, you can convert your input variables to integer or float using int()
or float()
and use the variables without curly braces.
import math
radius = float(input("hva er radiusen? "))
høyde = float(input("hva er høyden? "))
mOmkrets = (radius * math.tau)
Areal_sirkel = (math.pi * radius**2)
print("omkrets av høyden = ", math.tau * radius)
print("Areal av sirkelen = ", math.pi * radius**2)
print("Areal av sylinderen = ", math.tau * radius * høyde 2 * Areal_sirkel)
CodePudding user response:
Have you tried converting the variables to a common type? Try:
float(radius)
and float(høyde)
Probably python assigns integer to the inputs if you enter integer values.