【问题标题】:I want to calculate area of different shape on python我想在python上计算不同形状的面积
【发布时间】:2020-10-12 06:46:11
【问题描述】:

用户输入“圆3.5”或“矩形3 5”或“梯形3 5 7”(数字由用户决定)并输出面积。下面是代码,但是不能运行。

s=input("Input the shape and number:")
# for example,
# s="rect:5 3"
# s="cir:3.5"
#s="trapz:3 5 7"
cmd, para=s.split(:)
print(f"{cmd}->{para}")

if cmd == 'cir':
    r = float(para)
    print(f"area={r*r*3.14}")
elif cmd == 'rect':
    sw, sh = int(para.split())
    print(f"area={sw*sh}")
elif cmd == 'trapz':
    ul, bl, h = int(para.split())
    print(f"area={(ul+bl)*h/2}")
else:
    print("wrong input")

感谢 cmets。我也尝试了另一种方法来解决这个问题。代码是:

s=input("Input the shape and number:").split()

if s[0]=="cir":
    r = float(s[1])
    print(f'area={r*r*math.pi}')
elif s[0]=="rect":
    sw, sh = int(s[1]), int(s[2])
    print(f"area={sw*sh}")
elif s[0]=="trapz":
    ul, bl, h = int(s[1]), int(s[2]), int(s[3])
    print(f'area={(ul+bl)*h/2}')
else:
    print('Wrong input!')

【问题讨论】:

  • 欢迎来到 SO。您的代码中有一些问题。 1) 你正在覆盖你的变量s。 2) 尝试s.split(":") 请发布完整的错误回溯。
  • 大括号不用于分组,使用括号。
  • @Marichyasana 这里的大括号不用于分组,它们是 f-string 语法的一部分。
  • @Ryo kamili 使用 math.pi 而不是 3.14。

标签: python area


【解决方案1】:

您不能只将int 应用于列表

s=input("Input the shape and number:")
cmd, para=s.split(":")
print(cmd,"->",para)

if cmd=='cir':
    r = float(para)
    print(f"arear={r*r*3.14}")
elif cmd=='rect':
    sw, sh = (float(x) for x in para.split())
    print(f"area={sw*sh}")
elif cmd=='trapz':
    ul, bl, h = (float(x) for x in para.split())
    print(f"area={(ul+bl)*h/2}")
else:
    print("wrong input")

【讨论】:

    【解决方案2】:

    您的代码存在一些问题。在用户提供输入后,您正在为 s 分配一个值。我假设这些只是供您自己参考,所以我将它们注释掉。

    由于s.split() 会将您的字符串转换为列表,因此您需要确保将列表的某些部分分配给正确数量的变量。您不能直接将两个变量分配给三元素列表。所以我使用s.split(':')[0], s.split(':')[1:] 来获取列表的第一个元素,然后是同一列表的其余元素。

    此外,您不能将int() 应用于列表,因为 Python 不会为您带来那么大的魔力,因此您需要改用列表推导式。

    s=input("Input the shape and number:")
    # s="rect:5:3"
    # s="cir:3.5"
    # s="trapz:3 5 7"
    cmd, para=s.split(':')[0], s.split(':')[1:]
    print(cmd,"->",para)
    
    if cmd=='cir':
        r = float(para[0])
        print(f"arear={r*r*3.14}")
    elif cmd=='rect':
        sw, sh =[int(x) for x in para]
        print(f"area={sw*sh}")
    elif cmd=='trapz':
        ul, bl, h = [int(x) for x in para]
        print(f"area={(ul+bl)*h/2}")
    else:
        print("wrong input")
    

    示例输出:

    Input the shape and number:rect:5:3
    rect -> ['5', '3']
    area=15
    
    Input the shape and number:cir:3.5
    cir -> ['3.5']
    arear=38.465
    
    Input the shape and number:trapz:3:5:7
    trapz -> ['3', '5', '7']
    area=28.0
    

    【讨论】:

    • 非常感谢,现在我知道原因了。
    【解决方案3】:

    您要求输入并将他们的答案分配给此处的“s”变量:

    s=input("Input the shape and number:")
    

    那么您将 s 变量的值更改为不同的字符串 3 次。

    s="rect:5:3"
    s="cir:3.5"
    s="trapz:3 5 7"
    

    在这行代码之后,s 变量等于字符串:“trapz: 3 5 7”,无论用户输入什么。

    此外,用作“拆分”参数的冒号必须是字符串。

    【讨论】:

      【解决方案4】:

      试试:

      s=input("Input the shape and number:")
      s = s.split(':')
      cmd, para= s[0], s[1:] # <---- added this
      print(cmd,"->",para)
      
      if cmd=='cir':
          r = float(para[0])
          print(f"arear={r*r*3.14}")
      elif cmd=='rect':
          sw, sh =list(map(int, para)) #<---- added this
          print(f"area={sw*sh}")
      elif cmd=='trapz':
          ul, bl, h = list(map(int, para))
          print(f"area={(ul+bl)*h/2}")
      else:
          print("wrong input")
      

      Input the shape and number:trapz:3:5:7
      trapz -> ['3', '5', '7']
      area=28.0
      
      
      Input the shape and number:rect:3:5
      rect -> ['3', '5']
      area=15
      
      Input the shape and number:cir:4.6
      cir -> ['4.6']
      arear=66.44239999999999
      

      【讨论】:

        【解决方案5】:

        您的问题是多方面的。例如,您尝试将多个值传递给int()float(),它们接受并返回单个值。要将int 应用于列表,您可以使用map 函数,否则它将无法运行。

        我建议您查看在尝试运行此代码时收到的错误消息。在您学习 Python 时,这是一种很有价值的做法。

        【讨论】:

        • 您正在尝试将一个 int 和 float(它们是单个值)分配给几个变量para.split() 将返回一个包含两个值的列表,当分配给 @987654326 时@ 将被解包到这两个变量中。实际问题是 int 不接受列表,必须为该列表中的每个项目单独调用。
        • @AmalK 我编辑了我的评论以便更清楚。感谢您指出。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-14
        • 2021-07-07
        • 2018-10-06
        • 1970-01-01
        • 2014-02-16
        • 2013-08-12
        • 1970-01-01
        相关资源
        最近更新 更多