繁体   English   中英

我正在尝试制作一个程序来查找圆的面积,作为我在 python 中的第一个项目。有人知道我做错了什么吗?

[英]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?

我试过将 3.14 作为变量放入,但不确定哪里出了问题。 它总是给我同样的错误信息:

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'

我使用的代码是:

n = input()

Mn = (n * n) * 3.14

print(Mn)

第一行中的 input() 将返回一个字符串,因此您作为输入提供的任何值(例如 5)都将作为字符串存储在 n 中,您可以使用 type(n) 检查 n 的类型。

而是使用 int(input()) 这会将您传递的输入存储为 integer; 类似地,对于浮点输入,您可以使用 float(input())

解决您的问题:进行以下更改:

n = int(input())

Mn = (n * n) * 3.14

print(Mn)

您正在将一个字符串乘以一个字符串。 使用int(n) * int(n)int(n) ** 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM