我想计算python上不同形状的面积
用户输入“圆圈3.5”或“矩形35”或“梯形35 7”(数字由用户决定),并输出区域。下面是代码,但无法运行我想计算python上不同形状的面积,python,area,Python,Area,用户输入“圆圈3.5”或“矩形35”或“梯形35 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}"
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!')
你的问题是多方面的。例如,您试图将多个值传递给
int()
和float()
,这两个值接受并返回单个值。要将int
应用于列表,可以使用map
功能,否则它将无法运行
我建议您在尝试运行此代码时查看收到的错误消息。在学习Python的过程中,这是一个很有价值的实践。您不能只对列表应用
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")
您在此处请求输入并将其答案分配给“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”参数的冒号必须是字符串。您的代码有一些问题。用户提供输入后,您将为s赋值。我假设这些只是你自己的参考,所以我把它们注释掉了 由于
s.split()
将把字符串转换成一个列表,因此您需要确保将列表的各个部分分配给了正确数量的变量。不能直接将两个变量分配给三元素列表。因此,我使用了s.split(':')[0],s.split(':')[1://code>来获取列表的第一个元素,然后是同一列表的其余元素
此外,您不能将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
尝试:
欢迎来到SO。您的代码中有一些问题。1) 您正在覆盖变量s
。2) 尝试s.split(“:”)
请发布完整的错误回溯。花括号不用于分组,请使用括号。@这里的Marichyasana花括号不用于分组,它们是f字符串语法的一部分。@Ryo kamili使用math.pi
而不是3.14。您正在尝试分配一个int和float,它们是单值,对于多个变量,para.split()
将返回一个包含两个值的列表,当分配给sw,sh
时,这些值将被解包到这两个变量中。实际的问题是int
不接受列表,必须为该列表中的每个项目分别调用一个。@AmalK我编辑了我的注释以便更清楚。谢谢你的指点。非常感谢,现在我知道原因在哪里了。
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