[英]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.我试过将 3.14 作为变量放入,但不确定哪里出了问题。 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)
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).第一行中的 input() 将返回一个字符串,因此您作为输入提供的任何值(例如 5)都将作为字符串存储在 n 中,您可以使用 type(n) 检查 n 的类型。
instead use int(input()) this will store the input you pass as an integer;而是使用 int(input()) 这会将您传递的输入存储为 integer; similarly for float input you can use float(input())
类似地,对于浮点输入,您可以使用 float(input())
to fix your issue: make these changes:解决您的问题:进行以下更改:
n = int(input())
Mn = (n * n) * 3.14
print(Mn)
You are multiplying a string by a string.您正在将一个字符串乘以一个字符串。 Use
int(n) * int(n)
or int(n) ** 2
.使用
int(n) * int(n)
或int(n) ** 2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.