如何根据用户输入的内容打印一个多面体的体积?

How can I print the volume of one polyhedron based on what users inputs?

编写一个程序,读取多面体的第一个大写字母("T"、"C"、"O"、"D"或"I")的大小的边,它打印相应多面体的体积。如果读取的字母不是 5 个字母之一,您的程序将打印 "Unknown Polyhedron".

这几乎将每个输入值打印为 "else statement" 的值,但它应该根据用户输入的字母以及边的尺寸打印多面体的体积。

import math
p = str(input())
a = float(input())
T = (math.sqrt(2) / 12) * (a ** 3)
C = a ** 3
O = (math.sqrt(2) / 3) * (a ** 3)
D = (15 + 7 * math.sqrt(5)) / 4 * (a ** 3)
I = 5 * (3 + math.sqrt(5)) / 12 * (a ** 3)
if p == (math.sqrt(2) / 12) * (a ** 3) :
    print (T)
elif p == a ** 3 :
    print(C)
elif p == (math.sqrt(2) / 3) * (a ** 3) :
    print(O)
elif p == (15 + 7 * math.sqrt(5)) / 4 * (a ** 3) :
    print(D)
elif p == 5 * (3 + math.sqrt(5)) / 12 * (a ** 3) :
    print(I)
else :
    print('Polyèdre non connu')

这是您想要的更正工作版本

import math
p = input("Enter type of polyhedral: ")
a = float(input("Enter the value of a: "))

if p == 'T':
    volume =(math.sqrt(2) / 12) * (a ** 3)
elif p == 'C':
    volume = a**3
elif p == 'O':
    volume = (math.sqrt(2) / 3) * (a ** 3)
elif p == 'D':
    volume = (15 + 7 * math.sqrt(5)) / 4 * (a ** 3) 
elif p == 'I':
    volume =  5 * (3 + math.sqrt(5)) / 12 * (a ** 3)
else:
    print('Polyèdre non connu')
    volume = "Not Yet Defined"

print ("The volume of p=%s is %s" %(p, volume))      

示例输出

Enter type of polyhedral: O
Enter the value of a: 12.45
The volume of p=O is 909.71

Enter type of polyhedral: K
Enter the value of a: 12
Polyèdre non connu
The volume of p=K is Not Yet Defined