Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculating with float
#1
This code fails:

import math
r=input("r=")
h=input("h=")
V=math.pi*h*r**2
print (V)
V=math.pi*h*r**2
TypeError: can't multiply sequence by non-int of type 'float'


While this works:

import math
r=3.5
h=9
V=math.pi*h*r**2
print (V)
Can someone tell me where the difference lies and how to make the first bit of code work?

Thanks
Reply
#2
The built-in function returns a input.
Python is not JavaScript. Python is has a strong typing system.

Just convert the str to int or float:

f = float(input('Enter a float: '))
i = int(intput('Enter an int: '))

print(f+i)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 284 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,386 Jul-25-2019, 11:32 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020