KatanaXI
KatanaXI

Reputation: 11

The output is different from the output. What am I doing wrong?

I'm new to python programming. This is my assignment for my first final sem. If the user enter their facility as "1" then user as "Yes". The system should show the price accordingly. This is my code but, when my input is "2" and "yes" or any number with "yes", it's always showing the output for facility == 4.I don't know what I'm doing wrong here. It's really hard to find the mistake in this huge codes. Is there anyways to shorten this. If yes, do help me out.

if facility == 1 and user == "Yes" or user == "YES" or user == "yes":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 100
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 100
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 100
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 100
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 100
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 150
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 150

if facility == 2 and user == "Yes" or user == "YES" or user == "yes":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 100
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 100
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 100
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 100
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 100
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 150
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 150

if facility == 3 and user == "Yes" or user == "YES" or user == "yes":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 200
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 200
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 200
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 200
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 200
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 250
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 250

if facility == 4 and user == "Yes" or user == "YES" or user == "yes":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 30
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 30
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 30
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 30
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 30
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 60
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 60

if facility == 1 and user == "No" or user == "NO" or user == "no":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 150
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 150
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 150
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 150
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 150
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 200
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 200

if facility == 2 and user == "No" or user == "NO" or user == "no":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 150
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 150
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 150
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 150
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 150
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 200
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 200

if facility == 3 and user == "No" or user == "NO" or user == "no":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 250
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 250
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 250
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 250
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 250
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 300
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 300

if facility == 4 and user == "No" or user == "no" or user == "NO":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 35
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 35
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 35
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 35
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 35
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 65
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 65

Upvotes: 0

Views: 74

Answers (4)

Samwise
Samwise

Reputation: 71542

The problem is that and takes precedence over or. An expression like:

facility == 4 and user == "No" or user == "no" or user == "NO"

is the same as:

(facility == 4 and user == "No") or (user == "no") or (user == "NO"):

To have this expression do what you want it to do, you'd want to use something more like one of the following:

facility == 4 and (user == "No" or user == "no" or user == "NO")
facility == 4 and user in ("No", "no", "NO")
facility == 4 and user.lower() == "no"

Here is how I would write this logic more compactly -- use Enums to standardize all the string values (this protects you from typos and makes it easy to validate that the input is one of the expected values), and then put all the logic into a dict instead of a bunch of if/elif. The dict can be made more compact by the fact that you don't have individual prices for each day, simply different weekday/weekend prices:

from enum import Enum

class User(Enum):
    YES = "yes"
    NO = "no"

class Day(Enum):
    MONDAY = "monday"
    TUESDAY = "tuesday"
    WEDNESDAY = "wednesday"
    THURSDAY = "thursday"
    FRIDAY = "friday"
    SATURDAY = "saturday"
    SUNDAY = "sunday"
    def is_weekend(self) -> bool:
        return self in (Day.SATURDAY, Day.SUNDAY)


# Prices are keyed on facility number(int), User, and Day.is_weekend().
prices = {
    1: {User.YES: {False: 100, True: 150}, User.NO: {False: 150, True: 200}},
    2: {User.YES: {False: 100, True: 150}, User.NO: {False: 150, True: 200}},
    3: {User.YES: {False: 200, True: 250}, User.NO: {False: 250, True: 300}},
    4: {User.YES: {False: 30, True: 60}, User.NO: {False: 35, True: 65}},
}

price = prices[facility][User(user.lower())][Day(day.lower()).is_weekend()]

Upvotes: 1

Random Davis
Random Davis

Reputation: 6857

The problem is that your if statement logic is not correct. When you do the following check:

if facility == 4 and user == "Yes" or user == "YES" or user == "yes":

This is functionally equivalent to:

if (facility == 4 and user == "Yes") or (user == "YES") or (user == "yes"):

Meaning, if the user chose a variation of "Yes", then the if statement will always be entered. Your logic should look like this instead:

if facility == 4 and (user == "Yes" or user == "YES" or user == "yes"):

Which could be simplified to this, by just comparing the lowercase version of the user string:

if facility == 4 and user.lower() == "yes":

You could simplify all your logic to use lowercase, which would simplify your if statements to look like this:

if facility == 4 and user.lower() == "yes":
    if day.lower() == "monday":
        price = 30
    elif day.lower() ==  "tuesday":
        price = 30
    elif day.lower() == "wednesday":
        price = 30
    elif day.lower() == "thursday":
        price = 30
    elif day.lower() == "friday":
        price = 30
    elif day.lower() == "saturday":
        price = 60
    elif day.lower() == "sunday":
        price = 60

Upvotes: 0

Ugo Dos Reis
Ugo Dos Reis

Reputation: 79

To shorten this code you can use .lower() which turns a String into lowercase.

user = user.lower()
day = day.lower()

This will allow you to just check whether day == "monday" instead of looking through different capitalizations of Monday.

You could also do

if day in ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]

This will remove most of your elif statements and just put them all in one.

Upvotes: 0

Ginger
Ginger

Reputation: 303

Try using if/elif instead of if/if. Also, put brackets around the if logic, like so:

if facility == 4 and (user == "Yes" or user == "YES" or user == "yes":)

Upvotes: 0

Related Questions