简体   繁体   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?

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.

相关问题 我正在尝试在 python 中创建一个密码系统。 我不知道这有什么问题 - i am trying to make a password system in python. i dont know whats wrong with this 我正在尝试制作一个程序,但我不知道我做错了什么 - I am trying to make a program but i dont know what im doing wrong 在python中测量代码计时。 我怎么了 - Measure code timing in python. What did I wrong? 我的代码有什么问题? Python。 尝试制作游戏列表游戏 - What is wrong with my code? Python. Trying to make a gamelist game 尝试使用Python验证SHA1消息签名。我究竟做错了什么? - Trying to verify SHA1 message signature using Python. What am I doing wrong? 第一个python程序,我在做什么错 - first python program, what am i doing wrong 我正在尝试在我的 python 中的网络爬虫中索引一个 request.css。但它不起作用 - I am trying to index a request.css inside of my webscraper in python. But it does not work 我试图在 python 的 json 输出中获取与 name 关联的值。 我不知道该怎么做 - I am trying to get the values associated with name in my json output in python. I dont know how to do it 我做错了什么并且可以通过此计划改进吗? - Is there anything I did wrong and can improve with this program? 我在 python 函数中做错了什么? - What i did wrong in my python function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM