-1
length = input()
             
area = pow(length,2)
print(area)

In this we can give an integer as well as a float number so how we can solve this problem?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Why do you want to change a string into a float and an integer at once? What does this have to do with the code? And what does this have to do with `requests`? (that's for requests the *library*, not 'requests for help with python') – 2e0byo Sep 29 '21 at 15:44
  • You tagged this both Python 2 and 3, but in 3.x this is a TypeError (because input is always a string). It's not clear what you think the problem is. – jonrsharpe Sep 29 '21 at 15:46
  • well this is my first experience in stack overflow so I didn't knew about this sorry. – Sheryar Sher Sep 30 '21 at 14:25

2 Answers2

1

If your problem is 'how can I cast a str to the best numeric representation', try this:

x = input()
try:
    x = int(x)
except ValueError:
    x = float(x)
2e0byo
  • 5,305
  • 1
  • 6
  • 26
  • Note that this is a duplicate of [an answer](https://stackoverflow.com/a/379966/15452601) in the duplicate question. – 2e0byo Sep 29 '21 at 15:48
0

Here is the solution:

length = input().strip()
if "." in length:
    length = float(length)
else:
    length = int(length)
area = pow(length, 2)
print(area)
abhira0
  • 280
  • 1
  • 11