【问题标题】:how to make a radius of the circle using python如何使用python制作圆的半径
【发布时间】:2020-08-05 22:58:50
【问题描述】:

这是我不断出错的代码。编写一个程序,提示用户输入圆心和圆上一个点的坐标。然后程序应该输出圆的半径、直径、周长和面积。这是python的介绍类。

def main():    
    x1 = eval(input("enter x1"))
    y1 = eval(input("enter y1"))
    x2 = eval(input("enter x2"))
    y2 = eval(input("enter y2"))
    print((x2-x1)**2 + (y2-y1)**2)*(1/2)

pi = 3.14 
c = float(input("input the circumference of the circle :"))
print("the diameter of the circle with circumference" + str(c) + " is: " + str(2*pi*r))

r = float(input("input the radius of the circle :"))
print("the area of the circle with radius" + str(r) + " is: " + str(pi*r^2))

print("The radius,diameter,circumference,and area")

main()

【问题讨论】:

  • 你有什么错误?
  • c = float("input the circumference of the circle :") 应该是c = float(input("input the circumference of the circle :"))r= 也是如此
  • 我的错误是我的圆周给了我错误
  • 您的print("the diameter of the circle with circumference" + str(c) + " is: " + str(2*pi*r)) 语句在定义之前使用了“r”。
  • 您的代码存在不少于 4 种类型的错误,完整列表请参见我的答案。

标签: python


【解决方案1】:

函数内部和外部都有几个错误:

1) 您需要将输入转换为数字。更改此行和其他类似行:

x1 = eval(input("enter x1"))

到这里:

x1 = float(input("enter x1"))

2) ^ 操作符并没有像你想象的那样做,在 Python 中我们使用 ** 作为幂操作符。最好从函数中返回一个值,而不是打印结果。你应该替换这一行:

print((x2-x1)^2+(y2-y1)^2)^(1/2)

有了这个:

return ((x2-x1)**2 + (y2-y1)**2)**(1/2)

3) 你在定义它之前使用r,只需将此行移到第一行:

r = float("input the radius of the circle :")

4) 更改此行和与之类似的另一行:

c = float("input the circumference of the circle :")

到这里:

c = float(input("input the circumference of the circle :"))

【讨论】:

    【解决方案2】:

    圈码

    • 您错过了input 方法,您无法从字符串构建float
    • 电力运营商是**
    • 在定义变量之前不能使用变量(此处为r
    pi = 3.14
    c = float(input("input the circumference of the circle: "))
    r = float(input("input the radius of the circle: "))
    
    print("the diameter of the circle with circumference", c, "is:", str(2 * pi * r))
    print("the area of the circle with radius", r, "is:", str(pi * r ** 2))
    

    对于main 方法

    • eval更好的使用类型来获得你需要的类型x1 = float(input("enter x1"))
    • 你漏掉了一个右括号
    def main():
        x1 = float(input("enter x1"))
        y1 = float(input("enter y1"))
        x2 = float(input("enter x2"))
        y2 = float(input("enter y2"))
        print(((x2 - x1) ** 2 + (y2 - y1)) ** 2 ** (1 / 2))
        return ((x2 - x1) ** 2 + (y2 - y1)) ** 2 ** (1 / 2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 2022-11-29
      • 2021-12-30
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多