1

My problem is when I enter a number in the input field, it becomes number with special character. I don't know why it's adding extra characters to input. You can see the example. I entered just 5.2 and it became '5.2&

"""
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))
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&

Could you please help me?

gismgism
  • 15
  • 4
  • Could you share a little bit more of the code because `b = float(input("width: "))` in itself doesn't produce the error – KillerRebooted Nov 02 '22 at 16:11
  • I edited with the code, thank you for the response – gismgism Nov 02 '22 at 16:16
  • 1
    What do you mean by "prevent"? It's one thing to strip them out after they're entered. It's another thing to not let the user type them at all; and typically, normal expected behavior for terminal applications is just to prompt a second time when input is invalid. If someone pastes a UUID into a numeric field by mistake, you don't want to take only the numbers out of the UUID and treat that as if it's what they meant to enter; you want to let them enter a real, valid number. – Charles Duffy Nov 02 '22 at 16:17
  • Sorry, I may not express my problem clearly because obviously English is not my second language and I am a beginner in coding. What I am trying to say is when I run the code and I enter the input, it becomes the version I did not enter (as you can see in the example i entered 5.2 and it seems different). I am wondering why it is different? – gismgism Nov 02 '22 at 16:33
  • The example code runs fine. I posted code that separates the functions `input` and `float` to see which produces the error on your system. Let us know if and how you solve the issue because the posted code does not and should not produce the error. Sometimes a function is accidentally modified in a program but does not happen in this program. – Carl_M Nov 02 '22 at 17:10

2 Answers2

1

For me It's working fine...See my output..

Note: It throws error at last line in your code because you can't concatenate a str with an int.

So, you need to convert int to str before concatenating your variables

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

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

print("area: " + str(area) + " perimeter: " + str(perimeter))

#output

width: 5.2
5.2
area: 31.200000000000003 perimeter: 22.4

Alternate solution 2

Idk why it's adding extra chars to your user input...Anyway you can filter user input once entered as follows.

import re
a = 6
b = (input("width: "))
b=str(b)
b=re.sub("[^0-9^.]", "", b)

b=float(b)
print(b)

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

print("area: " + str(area) + " perimeter: " + str(perimeter))

output #

width: 5.2b
5.2
area: 31.200000000000003 perimeter: 22.4
0
"""
length of rectangle = a
width of rectangle = b
calculate the area and perimeter of the rectangle
"""

"""
We cannot replicate this error.
b = float(input("width: "))
ValueError: could not convert string to float: '5.2&

Debug by breaking down the code to see which function is the source of the 
error.
"""
a = 6
# b = float(input("width: "))
s = input("width: ")
print("s= " + s)
b = float (s)
area = a*b
perimeter = (2*a) + (2*b)

print("area: " + str(area) + " perimeter: " + str(perimeter))

Output

width: 5.2
s= 5.2
area: 31.200000000000003 perimeter: 22.4
Carl_M
  • 861
  • 1
  • 7
  • 14