Learning Python hit a snag on symple a simple program
I am just teaching myself python and trying to write a simple program. the goal is to calculate the number of pieces of equipment (HCAM) per 12 linear feet. I feel like I'm doing this the long way and I need to figure out how to write the equation so it only produces 1 answer if the feet are greater than 24. Basically I need "x" to increase by 1 for every 12 feet and only print 1 answer, this is what I have... Thanks for your patience/help.
x = 1
print 'Number of HCAMs Required'
feet = input ('enter linear feet of room: ')
if feet < 12:
print x, "HCAM Required"
if feet > 12:
print x + 1, "HCAM Required"
if feet > 24:
print x + 2, "HCAM R开发者_开发技巧equired"
Number of HCAMs Required
enter linear feet of room: 25
2 HCAM Required
3 HCAM Required
NOTE: IT SHOULD ONLY RESPOND "3 HCAM Required"
You missed the part on elif
.
25 is greater than 12, and greater than 24, so it prints both. You want to check the greater number first (24) and then use elif
(else if) so that it stops going through the other if statements.
As others have said before, your first issue is how you divide up your logic. In particular, when you want control to skip certain expressions, you must use elif:
if feet < 12:
...
elif feet > 12:
...
elif feet > 24:
...
else:
...
Next, there is a potential issue here. What if you have feet == 25? You'll notice that this code could potentially print multiple times. Remember that there are ways to check that multiple logical statements are true. So, lets say the value needs to be between 12 and 24 feet. One could say something like the following:
if feet >= 12 and feet <= 24:
print x
This helps one control the cases where other portions of the logic are used or not used.
Finally, one thing to keep in mind is this: what if the user enters a value larger than the values specified? For example, what if they enter feet == 240? It would be quite a task to write out every single condition possible (if it's even possible). As you said, the best thing for you to do would be come up with the formula to compute these values. In particular, think about how the amount of feet specified relates to the amount of feet within an HCAM. I think once you realize this, your logic will become much more straightforward!
Happy learning! :)
I'm pretty sure you're just looking for simple integer division here:
print 'Number of HCAMs Required'
feet = input ('enter linear feet of room: ')
print 1 + feet/12, "HCAM Required"
Integer division gives the result to the nearest integer and discards the remainder.
Number of HCAMs Required
enter linear feet of room: 25
3 HCAM Required
If you want to keep your original logical style, try this:
x = 1
print 'Number of HCAMs Required'
feet = input ('enter linear feet of room: ')
if feet <= 12:
print x, "HCAM Required"
elif 12 <= feet < 24:
print x + 1, "HCAM Required"
else:
print x + 2, "HCAM Required"
You can evaluate multiple comparison operators in one boolean expression, just be careful to exhaust all possibilities!
精彩评论