Why is this code not working correctly? Won't take the elif statements

Ben

This program is supposed to solve the problem of converting different units of length. For some reason the program only responds to the first "if" statement and runs the "meters_to_inches" and "meters_to_centimeters" functions only, even if the user inputs that they have inches or centimeters. If the user types in "meters" and then the length, it returns what it is supposed to, which is the length in inches and centimeters. But if the user wants to convert from inches or centimeters, it still uses the math from the meters functions and is not taking the elif statements. Can someone please help me?

#Title
print "Length Conversion"


#Description
print "This application will calculate the conversion of different units of length. There will be different conversion factors as described in the application."
print '\n'

#Directions
print "Hello. This application will help you convert into different units of lenth."
print '\n'
#Question
print "These are the different units to convert to and from:"
list = ["-Meters", "-Inches", "-Centimeters"]
for unit in list:
    print unit
print '\n'
#Question
units = raw_input("Which of these units do you have and would like to convert to one or both of the other two?")
print '\n'
amount = int(raw_input("How much of it do you have?"))
#Functions. One of these functions converts meters to inches, and the other converts meters to centimers. The original input gets muultiplied or divided by a certain number in order for it to be converted.
def meters_to_inches(amount):
  number = amount / 0.0254
  return number
def meters_to_centimeters(amount):
  number = amount * 100
  return number
#Functions. One of these functions converts inches to meters, and the other converts inches to centimers. The original input gets muultiplied or divided by a certain number in order for it to be converted.
def inches_to_meters(amount):
  number = amount * 0.0254
  return number
def inches_to_centimeters(amount):
  number = amount * 2.54
  return number
#Functions. One of these functions converts centimeters to meters, and the other converts centimeters to inches. The original input gets muultiplied or divided by a certain number in order for it to be converted.
def centimeters_to_meters(amount):
  number = amount / 100
  return number
def centimeters_to_inches(amount):
  number = amount / 2.54
  return number

if units == "Meters" or "meters":
  print meters_to_inches(amount)
  print "(inches)"
  print'\n'
  print meters_to_centimeters(amount)
  print "(centimeters)"
elif units == "Inches" or "inches":
  print inches_to_meters(amount)
  print "(meters)"
  print'\n'
  print inches_to_centimeters(amount)
  print "(centimeters)"
elif units == "Centimeters" or "centimeters":
  print centimeters_to_inches(amount)
  print "(inches)"
  print '\n'
  print centimeters_to_meters(amount)
  print "(meters)"
else:
  print "The unit you have given does not match a conversion this application provides"
boaz_shuster

Your conditions aren't right.

The following condition will always produce True in Python:

if variable == 'value' or 'other-value'

My guess is that you have meant to do this:

if variable == 'value1' or variable == 'value2'`

Thus, use upper or lower string's method to avoid these ors. For example,

if unit.upper() == 'INCHES'

Why does it always produce True?

If you excute the following code:

if 2:
    print 'yes!'
if 0:
    print 'no!'

You will see that Python prints only yes!. That means that the Boolean expression of any number but 0 is True in Python. Now, since you know that or is used to separate between two Boolean expressions, then by using this if-statement:

if x == 3 or 2

You know that 2 is True and since it's enough that one of the Boolean expressions is True when using or to execute the code-lines related to that statement.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why won't the routing work in this code?

분류에서Dev

Why isn't my java code proceeding past my if statements?

분류에서Dev

Why isn't my inner loop working correctly?

분류에서Dev

Code won't write to CSV file Python3, why?

분류에서Dev

Why won't the Tooltip appear correctly when i hover the mouse over the Kendosparkline?

분류에서Dev

Core data won't save correctly

분류에서Dev

I'm Working with a button command, but can't see what's wrong with this code using elseif statements

분류에서Dev

Why won't DNS resolve?

분류에서Dev

Why won't this code display Error: Could not find file when e is thrown by a nonexistant file?

분류에서Dev

For loop with if statements isn't working as expected in bash

분류에서Dev

php Cookie not working, won't get set

분류에서Dev

Why is the Pay rent Method Not working Correctly?

분류에서Dev

I'm trying to get jquery working in the background.js of my chrome extension and it won't work. why?

분류에서Dev

Cursor is an X and panel isn't working correctly

분류에서Dev

Why won't this regexp in htaccess work properly?

분류에서Dev

Why won't jQuery remove my class?

분류에서Dev

Why Won't the BG Colors Show Up?

분류에서Dev

Why won't this MySQL stored procedure work?

분류에서Dev

Why won't YouTube in Firefox prevent the screensaver?

분류에서Dev

Why won't the terminal display new lines?

분류에서Dev

Why won't dhcpd listen on the virtual interface

분류에서Dev

Python if elif code

분류에서Dev

Python: Why isn't this working?

분류에서Dev

Removing Javascript alert line stops my append code working correctly

분류에서Dev

Why decode using base64 not working correctly in C#

분류에서Dev

Why is my js only working correctly when the page is refreshed?

분류에서Dev

React: Why won't this basic data populate in my functional component?

분류에서Dev

Why std::basic_fstream<unsigned char> won't work?

분류에서Dev

Why won't my express js app execute?

Related 관련 기사

  1. 1

    Why won't the routing work in this code?

  2. 2

    Why isn't my java code proceeding past my if statements?

  3. 3

    Why isn't my inner loop working correctly?

  4. 4

    Code won't write to CSV file Python3, why?

  5. 5

    Why won't the Tooltip appear correctly when i hover the mouse over the Kendosparkline?

  6. 6

    Core data won't save correctly

  7. 7

    I'm Working with a button command, but can't see what's wrong with this code using elseif statements

  8. 8

    Why won't DNS resolve?

  9. 9

    Why won't this code display Error: Could not find file when e is thrown by a nonexistant file?

  10. 10

    For loop with if statements isn't working as expected in bash

  11. 11

    php Cookie not working, won't get set

  12. 12

    Why is the Pay rent Method Not working Correctly?

  13. 13

    I'm trying to get jquery working in the background.js of my chrome extension and it won't work. why?

  14. 14

    Cursor is an X and panel isn't working correctly

  15. 15

    Why won't this regexp in htaccess work properly?

  16. 16

    Why won't jQuery remove my class?

  17. 17

    Why Won't the BG Colors Show Up?

  18. 18

    Why won't this MySQL stored procedure work?

  19. 19

    Why won't YouTube in Firefox prevent the screensaver?

  20. 20

    Why won't the terminal display new lines?

  21. 21

    Why won't dhcpd listen on the virtual interface

  22. 22

    Python if elif code

  23. 23

    Python: Why isn't this working?

  24. 24

    Removing Javascript alert line stops my append code working correctly

  25. 25

    Why decode using base64 not working correctly in C#

  26. 26

    Why is my js only working correctly when the page is refreshed?

  27. 27

    React: Why won't this basic data populate in my functional component?

  28. 28

    Why std::basic_fstream<unsigned char> won't work?

  29. 29

    Why won't my express js app execute?

뜨겁다태그

보관