'I am trying to make a program that finds the area of a circle as my first project in python. does anything know what I did wrong?
I have tried putting 3.14 in as a variable, and am not sure what is wrong. It always gives me the same error message:
Traceback (most recent call last):
File "/Users/[REDACTED]/PycharmProjects/Pi/main.py", line 2, in <module>
Mn = (n * n) * 3.14
TypeError: can't multiply sequence by non-int of type 'str'
The code I am using is:
n = input()
Mn = (n * n) * 3.14
print(Mn)
Solution 1:[1]
the input() in the first line will return a string so any value you are giving as input for example 5 will be stored in n as a string, you can check the type of n by using type(n).
instead use int(input()) this will store the input you pass as an integer; similarly for float input you can use float(input())
to fix your issue: make these changes:
n = int(input())
Mn = (n * n) * 3.14
print(Mn)
Solution 2:[2]
You are multiplying a string by a string. Use int(n) * int(n) or int(n) ** 2.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Siddhant Ghungrudkar |
| Solution 2 | Shib |
