0

So basically I'm trying to add a conditional statement, a simple yes or no one. When this function runs it is expected to skip over the inputs if it evaluates to false but it runs it anyways regardless rather it's false or true. Whats?!?!

def Add_Expense_Owedbill(bill,expense):
 billinfo = []
 if bill == "y" or "Y": 
  name = input("name "),
  date = input("date "),
  past_due = input("past due "),
  total = input("total "),
  address= input("address if one "),    
  phone = input("phone number if availiable ")
  billinfo.append(name)
  billinfo.append(date)1
  billinfo.append(past_due)
  billinfo.append(total)
  billinfo.append(address)
  billinfo.append(phone)
 else:
  pass

Also... if you have any advice on how I can improve my code to make it more concise and efficient that would be great!

  • 3
    `if bill == "y" or bill == "Y":` – Thomas Schillaci Jun 25 '20 at 18:58
  • Alternatively, `if bill in ("y", "Y"):` –  Jun 25 '20 at 18:59
  • 2
    One space for indentation is really hard to read. [PEP-8](https://www.python.org/dev/peps/pep-0008/) specifies [4 spaces](https://www.python.org/dev/peps/pep-0008/#indentation). – Fred Larson Jun 25 '20 at 19:02
  • 2
    Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Fred Larson Jun 25 '20 at 19:05

1 Answers1

1

Let's look at the if statement:

if bill == "y" or "Y":

What the or operator does is evaluate each side separately, starting with the left.

If bill == "y" returns false, it will evaluate "Y" as its own expression, which will always return true. It does not compare it to bill.

To fix this, you can do one of two things:

  1. Add the comparison to the right side
  2. Use a list

Comparison:

if bill == "y" or bill == "Y":

List:

if bill in ["y", "Y"]:

For more information, see answers to this question.

Marsroverr
  • 556
  • 1
  • 6
  • 23