为了账号安全,请及时绑定邮箱和手机立即绑定

我想在python上计算不同形状的面积

我想在python上计算不同形状的面积

泛舟湖上清波郎朗 2023-10-31 16:12:07
用户输入“圆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")感谢您的评论。我也尝试其他方法来解决这个问题。代码是: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!')
查看完整描述

5 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

你不能只申请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")


查看完整回答
反对 回复 2023-10-31
?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

您的代码有一些问题。您在用户提供输入后为 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


查看完整回答
反对 回复 2023-10-31
?
拉丁的传说

TA贡献1789条经验 获得超8个赞

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

我建议您查看尝试运行此代码时收到的错误消息。这是学习 Python 时非常有价值的实践。


查看完整回答
反对 回复 2023-10-31
?
噜噜哒

TA贡献1784条经验 获得超7个赞

尝试:


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


查看完整回答
反对 回复 2023-10-31
?
侃侃无极

TA贡献2051条经验 获得超10个赞

您要求输入并将其答案分配给此处的 's' 变量:


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

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


s="rect:5:3"

s="cir:3.5"

s="trapz:3 5 7"

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


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


查看完整回答
反对 回复 2023-10-31
  • 5 回答
  • 0 关注
  • 98 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信