0

在编程中,我正在编写用户选择实体的代码,然后计算体积和表面积。但我没有直接选择,而是让用户选择询问有关实体的额外信息,如果他们输入数字然后“显示”(例如“1 显示”)。他们可以一直询问直到选择了一个实体,所以我使用了一个 while 循环。

循环不工作。如果带有循环的条件无效,它仍然会进入它并循环并且不让我出去。请帮忙。我认为这很简单。我试图在没有帮助的情况下进行整数和字符串转换。

#11/07/12 
#A program that asks user for input and calculates SA and Volume of a chosen solid

print "Which solid would you like to calculate?"    
print "  "      
print "1. Tetrahedron"          
print "2. Dodecahedron"           
print "3. Cone"                 
print "4. Sphere"                        
print "5. Icosahedron"                    
print '  '                        
print "If you want extra information on the solid, enter the solid's number and then add show. Example: 1 show"                   
print "If not, just enter the number"                       
choice = raw_input('----->')                             

#Where asks if user wants extra information on the solids, loops until solid is chosen             
while choice != '1' or choice != '2' or choice != '3' or choice != '4' or choice != '5':      
    if choice == "1 show":     
        print "A tetrahedron is composed of four congruent triangle faces. "     
    if choice =='2 show':      
        print "A dodecahedron is a polyhedron composed of 12 pentagonal faces. "      
    if choice == '3 show':       
        print 'A cone is a geometric solid that tapers smoothly from a circular base to an   apex. '          
    if choice == '4 show':          
        print "A sphere is a perfectly round circle. "            
    if choice == '5 show':
        print 'An icosahedron is a regular polyhedron with 20 congruent equilateral triangular faces'        
    choice = raw_input('Your new choice: ')              

if choice == 1: # Tetradedron               
    tetraside = raw_input("What is the length of your solid's side? ")               
    tetrabaseA = ((3 ** (1/2)) / 4) * tetraside**2                
    tetraheight = 9 * ((6 ** (1/2)) / 3) * tetraside                  
    tetraSA = 4 * tetrabaseA                
    tetraV = (1 / 3) * tetrabaseA * tetraheight                  
4

2 回答 2

5

你的while条件不对。想一想:即使choiceequals '1',条件choice != '2'也会为真,所以总是会满足整个条件。

你需要类似的东西

while choice not in {'1', '2', ...}:

或者

while choice not in set(map(str, range(1, 6))): # a fancier version

或者只是将所有更改orand您的状态。

于 2012-11-11T20:43:39.337 回答
2

请注意,虽然您的错误出现在循环条件中,但您的其他一些代码可以通过使用字典查找而不是大块来变得更加“Pythonic” if elif else

例如,您的 while 循环可以简化为:

info = { "1 show" : "A tetrahedron is composed of four congruent triangle faces.",
         "2 show" : "A dodecahedron is a polyhedron composed of 12 pentagonal faces.",
         "3 show" : "A cone is a geometric solid that tapers smoothly from a circular base to an   apex.",
         "4 show" : "A sphere is a perfectly round circle.",
         "5 show" : "An icosahedron is a regular polyhedron with 20 congruent equilateral triangular faces" }

while choice not in { "1", "2", "3", "4", "5" }:
    print info.get(choice, "I'm sorry, I didn't understand your input.")
    choice = raw_input('Your new choice: ')

您可能稍后可以对体积和表面积的实际计算执行类似的操作,尽管您可能需要将这些计算的代码放入单独的函数中,然后将函数放入字典(甚至是列表,因为索引将是整数)。

于 2012-11-11T21:27:31.617 回答