While loops help
Hi for my while loops homework I got a question which I don't really understand what to write, so I would be grateful if someone could explain it to me:
A parts supply company uses 4-digit part numbers. The last digit indicates the production run. If
the production run is 6,7 or 8 it is considered to be an old model.
Write a code that prompts the user to enter a part number.
The length of the part number should be equal to 4 digits, otherwise an error message will be
displayed and the user will be prompted to input the part number again.
The algorithm should count the total number of parts entered and the number of an old model
parts and output these totals.
Data input will terminate when the user inputs 9999.
OK, so i cant give you all the code, but i can help you do it. Im gonna assume you know the basics of python.
first of all, you need an input to get the 4 digit number.
Next, we need to find out if the number is a 4 digit number. this can be used with
len(variable_name) and replace
variable_name with the variable that stocked that input.
you can now use an
else statement to see if
len(variable_name) == 4. If it isnt, then print an error message.
so now we know how many digits the number has, we have to find out the last digit. this can be achieved with the modulo
%. Basically, if you do a number % 10, it returns the last digit of that number. for example, 1234 % 10 = 4 and 4 is the last digit.
So all you have to do is
variable_name % 10 which will find the last digit, and then use an
elif statement to determine if that number (the production run) is a
Then add a print statement to print if its an old model or not.
Finally, we need a
while statement at the start, to see if the number entered by the user is 4 digits or not. So if the length of the number is 4, then you can put
break to break the loop (meaning that the condition was verified), and if it isnt a 4 digit number, use
else and then
pass which will go back to the start (where there is the input)
So i hope you understand, if not, I can keep on helping (I can give you a bit of the code ;)
Happy coding and hope this helps! =)