Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7128639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:09:34+00:00 2026-05-28T11:09:34+00:00

I am new to Python and I do not know why but the if

  • 0

I am new to Python and I do not know why but the if, elif in the following code is not working as I expect it to. However,

  • It works perfectly when I type 1 to 7

  • it works perfectly when I type 0 8 or 9 (it says “Try again”)

  • It does not work if I type 10 to 69, 100 to any number

When I say it does not work I mean it prints

my_shape_num = h_m.how_many()

But I do not know why. It has to stop if choice is not between 1 and 7

def main(): # Display the main menu
    while True:
        print
        print "  Draw a Shape"
        print "  ============"
        print
        print "  1 - Draw a triangle"
        print "  2 - Draw a square"
        print "  3 - Draw a rectangle"
        print "  4 - Draw a pentagon"
        print "  5 - Draw a hexagon"
        print "  6 - Draw an octagon"
        print "  7 - Draw a circle"
        print
        print "  X - Exit"
        print

        choice = raw_input('  Enter your choice: ')

        if (choice == 'x') or (choice == 'X'):
            break

        elif (choice >= '1' and choice <= '7'):
            my_shape_num = h_m.how_many()
            if ( my_shape_num is None): 
                continue

            d_s.start_point() # start point on screen

            if choice == '1': 
                d_s.draw_triangle(my_shape_num) 
            elif choice == '2': 
                d_s.draw_square(my_shape_num) 
            elif choice == '3':             
                d_s.draw_rectangle(my_shape_num) 
            elif choice == '4':             
                d_s.draw_pentagon(my_shape_num) 
            elif choice == '5':             
                d_s.draw_hexagon(my_shape_num) 
            elif choice == '6':             
                d_s.draw_octagon(my_shape_num) 
            elif choice == '7': 
                d_s.draw_circle(my_shape_num)

        else:
            print
            print '  Try again'
            print

Edit: Ok, sorted:

choice = raw_input('  Enter your choice: ')

if (choice == 'x') or (choice == 'X'):
    break


try:
    choice = int(choice)
    if (1 <= choice <= 7):

        my_shape_num = h_m.how_many()
        if ( my_shape_num is None): 
            continue

        d_s.start_point() # start point on screen

        if choice == 1: 
            d_s.draw_triangle(my_shape_num) 
        elif choice == 2: 
            d_s.draw_square(my_shape_num) 
        elif choice == 3:             
            d_s.draw_rectangle(my_shape_num) 
        elif choice == 4:             
            d_s.draw_pentagon(my_shape_num) 
        elif choice == 5:             
            d_s.draw_hexagon(my_shape_num) 
        elif choice == 6:             
            d_s.draw_octagon(my_shape_num) 
        elif choice == 7: 
            d_s.draw_circle(my_shape_num)

    else:
        print
        print '  Number must be from 1 to 7!'
        print

except ValueError:
    print
    print '  Try again'
    print
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:09:34+00:00Added an answer on May 28, 2026 at 11:09 am

    Strings are compared lexicographically: '10' is greater than '1' but less than '7'. Now consider this code:

    elif (choice >= '1' and choice <= '7'):
    

    In addition to accepting '7', this will accept any string beginning with 1, 2, 3, 4, 5 or 6.

    To fix, convert choice to integer as soon as you’ve tested for 'x', and use integer comparisons thereafter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to wxPython (but not Python itself), so forgive me if I've
I am new to Python, and I'm working on writing some database code using
I'm relatively new to python and know very little syntax, but I'm willing to
I am not that really new in Programming but in Python I am really
I will confess I'm very new to Python and I don't really know what
Let me start off by saying, I'm not new to programming but am very
Hey I am very new to python but I am trying to make a
I am pretty new in python, and I have a problem I don't know
I am new to Python and Web.py, but I am tearing my hair out
New to python, competent in a few languages, but can't see a 'snazzy' way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.