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状態は間違っています。考えてみてください。choiceが等しい場合でも'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

if elif else間違いがループ状態にある間は、大きなブロックではなく辞書ルックアップを使用することで、他のコードの一部をより「Pythonic」にすることができることに注意してください。

たとえば、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 に答える