Home > OS >  ValueError: could not convert string to float?
ValueError: could not convert string to float?

Time:11-03

I am a beginner in terms of coding. Python gives me the same error whenever I try to do calculation. I use python 3.9. I do not make typo when I enter input (this one was 5.2). Could you please help me?

"""
length of rectangle = a
width of rectangle = b
calculate the area and perimeter of the rectangle
"""

a = 6
b = float(input("width: "))

area = a*b
perimeter = (2*a)   (2*b)

print("area: "   str(area)   " perimeter: "   str(perimeter))
Traceback (most recent call last):
  File "c:\Users\HP\OneDrive - Karabuk University\Belgeler\Downloads\first.py", line 8, in <module>
    b = float(input("width: "))
ValueError: could not convert string to float: '5.2&

CodePudding user response:

No problem with the code, not the code, whether your input key has some hidden characters attached

┌──[[email protected]]-[/]
└─$vim demo.py
┌──[[email protected]]-[/]
└─$python3 demo.py
width: 5.2
area: 31.200000000000003 perimeter: 22.4
┌──[[email protected]]-[/]
└─$cat demo.py
"""
length of rectangle = a
width of rectangle = b
calculate the area and perimeter of the rectangle
"""

a = 6
b = float(input("width: "))

area = a*b
perimeter = (2*a)   (2*b)

print("area: "   str(area)   " perimeter: "   str(perimeter))
┌──[[email protected]]-[/]
└─$

CodePudding user response:

This code should work. I guess the problem is your input: '5.2&'. You made a typo here. Revove '&' and it will work.

CodePudding user response:

"""
length of rectangle = a
width of rectangle = b
calculate the area and perimeter of the rectangle
"""

a = 6
b = float(input("width: "))

area = a*b
perimeter = (2*a)   (2*b)

print("area: " , area , " perimeter: " , perimeter)
  • Related