0

In programming, I'm writing a code that the user chooses a solid, and then it calculates the volume and surface area. But instead of choosing directly, I give the user the option to ask for extra information on the solid, if they enter the number then 'show' (for example '1 show'). They can keep asking until a solid is chosen, so i used a while loop.

The loop isn't working. If the condition with the loop isn't valid, it still goes into it and loops and doesn't let me get out. Please help. It's pretty simple I think. I have tried to do integer and string conversions, without it helping.

#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                  
1
  • 4
    If some part of your code is irrelevant ("ignore the time delays", you say), just delete it from your question. Make it as concise and readable as you can (use the edit link). Commented Nov 11, 2012 at 20:45

2 Answers 2

5

Your while condition is wrong. Think about it: even if choice equals '1', the condition choice != '2' will be true, so the whole condition will always be satisfied.

You need something like

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

or

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

or just change all or to and in your condition.

Sign up to request clarification or add additional context in comments.

3 Comments

I think you've made a typographical error: you've used curly braces and omitted a colon in while choice not in ('1', '2', ...):.
@poorsod That's a set literal (works in Python >= 2.7). See here for documentation.
Thank you so much for pointing out that mistake. i tried changing the or's to and. the same problem still occured. but once i made the list, and just changed the rest of the code a little bit it works great. you get creds when i get 100% on this :)
2

Note that while your mistake was in the loop condition, some of your other code could be made more "Pythonic" by using dictionary lookups rather than big if elif else blocks.

Your while loop, for instance, could be simplified to this:

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: ')

You can probably do something similar later on for the actual computation of the volume and surface area, though you might need to put the code for those calculations into separate functions and then put the functions into a dictionary (or even a list, since the indexes will be integers).

1 Comment

Note that PEP-8 recommends against spaces in literals and before the colon in a dict.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.