0

My assignment is:

You have been asked to write a program that will give the name of a shape depending on the number of sides. The user can only enter numbers between 3 and 8, if they enter any other number then the program should tell them to enter a number between 3 and 8.

And here's my Python answer:

#Sides and shapes
sides = int(input("How many sides on the shape are there? "))
if sides ==3:
    print ("Your shape is the triangle")
if sides ==4:
    print ("Your shape is the square")
if sides ==5:
    print ("Your shape is the pentagon")
if sides ==6:
    print ("Your shape is the hexagon")
if sides ==7:
    print ("Your shape is the heptagon")
if sides ==8:
    print ("Your shape is the octagon")
elif sides != range(3,9):
    print ("You should enter a number between 3 and 8")

After the elif statement I somehow also need to loop it so that if the person inputs other than 3-8 then it will keep asking for them to enter a number from 3 to 8.

The elif statement does not work for some reason and outputs this answer in F5:

How many sides on the shape are there? 6
Your shape is the hexagon
You should enter a number between 3 and 8
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Draguno
  • 13
  • 5

6 Answers6

3

range() produces an object of a different type; integer != range() is always going to be true.

Either test if you integer is outside a range with < or <= and chaining:

elif not (3 <= sides < 9):
    print ("You should enter a number between 3 and 8")

or use not in to see if the number is outside the range:

elif sides not in range(3, 9):
    print ("You should enter a number between 3 and 8")

or simply use elif on all your tests but the first, and else for the last branch; it'll picked only if the if..elif tests did not match:

if sides ==3:
    print ("Your shape is the triangle")
elif sides ==4:
    print ("Your shape is the square")
elif sides ==5:
    print ("Your shape is the pentagon")
elif sides ==6:
    print ("Your shape is the hexagon")
elif sides ==7:
    print ("Your shape is the heptagon")
elif sides ==8:
    print ("Your shape is the octagon")
else:
    print ("You should enter a number between 3 and 8")

Note that there is only one if now; logically, the elif and else parts belong to that one if statement. Any other if forms a separate, new set of choices, and your sides != range(3, 9) expression was always true, meaning the elif test would be true any time if slides == 8 was not true.

You can simplify your code by using a dictionary. It lets you associate a key with a value; make the numbers your keys, and you can simply test if sides is in the dictionary, or return a default value if it is not:

shape_msg = "Your shape is the "
result = {
    3: shape_msg + "triangle",
    4: shape_msg + "square",
    5: shape_msg + "pentagon",
    6: shape_msg + "hexagon",
    7: shape_msg + "heptagon",
    8: shape_msg + "octagon",
}

sides = int(input("How many sides on the shape are there? "))
result = results.get(sides, "You should enter a number between 3 and 8")
print(result)

Here, the dict.get() method returns the value for the given key, or the default value if the key is not present.

If you need to continue the loop, test for the presence of the key and branch based on that:

while True:
    sides = int(input("How many sides on the shape are there? "))
    if sides in result:
        print(sides[result])
        break  # done, exit the loop
     print("You should enter a number between 3 and 8")

For further tips on how to ask a user for input and handle their wrong input, see Asking the user for input until they give a valid response.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you this worked for me. #Sides and shapes result = { 3: "Your shape is the triangle", 4: "Your shape is the square", 5: "Your shape is the pentagon", 6: "Your shape is the hexagon", 7: "Your shape is the heptagon", 8: "Your shape is the octagon", } sides = int(input("How many sides on the shape are there? ")) result = result.get(sides, "You should enter a number between 3 and 8") print(result) – Draguno Jan 27 '16 at 19:03
  • 2nd assignment posted, problems. – Draguno Jan 27 '16 at 19:22
  • Post a **new** question for a new problem please. – Martijn Pieters Jan 27 '16 at 19:25
  • I have to wait 90 minutes :( – Draguno Jan 27 '16 at 19:27
  • Yes, and those limits are there for a reason. We aim to be a collection of quality questions and even better answers with lasting value for future visitors, not a one-on-one mentoring facility. Try to work out your problem on your own in the meantime. – Martijn Pieters Jan 27 '16 at 19:29
  • Worked on it and can get it out put late and never but with the on time its hard as it has a space and its two worded. – Draguno Jan 27 '16 at 19:35
1

Your elif is only "associated" with the previous if, so it will hit that elif for anything that is not an octagon, then as others have noticed your comparison is not testing if you're in the right range, you're comparing an int against a list.

Probably you really want all of your if statements except the first to be elif and the one you have as an elif to be the else clause for that whole block.

Better still, you could have a dictionary that maps the number of sides to the message, and if you don't have a key in the dictionary you can print the error message.

For the loop, if you put the whole thing in while True you could break after each success and not after the invalid number of sides, which would cause it to repeat the loop until they finally enter a valid number.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
0

range(x, y) outputs a range of numbers between x and y. You're then comparing an integer (which is the value of sides) to a list. Obviously it isn't equal to the list, because it is an integer!

Alex Alifimoff
  • 1,850
  • 2
  • 17
  • 34
0

Look at last if 6!= [3,4,5,6,7,8] , so your code will enter elif statement and print ("You should enter a number between 3 and 8") will be executed.

Farseer
  • 4,036
  • 3
  • 42
  • 61
0

You've already covered all the legal answers with your if and elif statements, so there's no need to do any specific checking of other values.

Just change the last statement into an else:

else:
    print ("You should enter a number between 3 and 8")
John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

I would do something like this (and it seems to work) :

invalidAnswer = True
while invalidAnswer :
    invalidAnswer = False
    sides = int(input("How many sides on the shape are there? "))
    if sides ==3:
        print ("Your shape is the triangle")
    elif sides ==4:
        print ("Your shape is the square")
    elif sides ==5:
        print ("Your shape is the pentagon")
    elif sides ==6:
        print ("Your shape is the hexagon")
    elif sides ==7:
        print ("Your shape is the heptagon")
    elif sides ==8:
        print ("Your shape is the octagon")
    else :
        print ("You should enter a number between 3 and 8")
        invalidAnswer = True