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 in Pycharm:
Traceback (most recent call last):
File "/Users/[REDACTED]/PycharmProjects/Pi/main.py", line 2, in 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)
CodePudding user response:
You are multiplying a string by a string. Use int(n) * int(n)
or int(n) ** 2
.
CodePudding user response:
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)