![](/img/trans.png)
[英]i am trying to make a password system in python. i dont know whats wrong with this
[英]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.