Inputs should be multiples of 12

This is the place for queries that don't fit in any of the other categories.

Inputs should be multiples of 12

Postby pc10015 » Fri Apr 04, 2014 4:22 am

I'm totally new to python and i'm making a program using python that will ask the user to enter the number of chocolates they would like to purchase. But the problem is how can i get the user to input the number of chocolates in multiples of 12, so basically a dozen. If the input is not a multiple of 12 then ask them to re-enter the number of chocolates in multiples of 12. Please help me.
Last edited by Mekire on Fri Apr 04, 2014 4:24 am, edited 1 time in total.
Reason: First post lock.
pc10015
 
Posts: 8
Joined: Fri Apr 04, 2014 4:14 am

Re: Inputs should be multiples of 12

Postby Mekire » Fri Apr 04, 2014 4:26 am

You are going to need a while loop where you get the input. THen check if the input is evenly divisible by twelve (probably using the modulus operator). If this is satisfied, break out of the loop.

This is simple enough, but I would like to see an attempt before I hand you any code.
-Mek
User avatar
Mekire
 
Posts: 1008
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Inputs should be multiples of 12

Postby pc10015 » Fri Apr 04, 2014 7:11 am

Mekire Thank you for your advice. It worked. Here is what i got.

Code: Select all
numberChocolate=0
while numberChocolate!="q":
    numberChocolate=int(input("Enter number of chocolates you would like to buy:"))
    if numberChocolate%12:
        print("Enter the number of chocolate in a dozen.")
    else:
        print("Thank you")
        break
pc10015
 
Posts: 8
Joined: Fri Apr 04, 2014 4:14 am

Re: Inputs should be multiples of 12

Postby Mekire » Fri Apr 04, 2014 8:14 am

Pretty good. You still need to add some error checking though (probably with try/except). Currently your program will crash if it is given anything other than an integer. Even 'q' which you intended to quit the loop actually crashes the program.

-Mek
User avatar
Mekire
 
Posts: 1008
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests