Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IF statement trouble
#1
Hi, I am fairly new to python and coding in general. I was trying to practice a bit by making a cook book sort of thing and I can't figure out what I am doing wrong. Every time I run the code and enter the raw_input, I only get the response under the "chicken Parmesan" section of the if statement, even if I type "buttcheeks" as the raw input. Help pls! Code is below:


def cook_book():
  meal = raw_input("What meal would you like to cook?")
  if meal == 'Chicken Parmesan' or 'Chicken Parm': 
    print "Ingredients: " + str(ingredients_and_recipes['Chicken Parmesan'][0])
    print "Instructions: " + str(ingredients_and_recipes['Chicken Parmesan'][1]) 
  elif meal == 'Crepes' or 'Crepe':
    print "Ingredients: " + str(ingredients_and_recipes['Crepes'][0])
    print "Instructions: " + str(ingredients_and_recipes['Crepes'][1])
  else:
    print "Sorry. That is not in Eoin's Cook Book"
Reply
#2
https://python-forum.io/Thread-Multiple-...or-keyword
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks so much!
Reply
#4
I can only get one outcome from this if statement. Even if the value of total_cost_car_gas is larger than budget_car_and_gas, the code will still run the difference_under operation and print the things in that section of the if statement. Is there something wrong with how I constructed the IF statement?

def bool_one(budget_car_and_gas):
  total_cost_car_gas = cost_of_car_1(days_car_1) + cost_of_gas(distance_travelling)
  if total_cost_car_gas > budget_car_and_gas :
    difference_over = float(total_cost_car_gas) - float(budget_car_and_gas)
    print "It seems you are $%s NZ over your budget." % (difference_over)
  elif total_cost_car_gas == budget_car_and_gas :
    print "Whew! You are exactly on your budget!"
  elif total_cost_car_gas < budget_car_and_gas :
    difference_under = float(budget_car_and_gas) - float(total_cost_car_gas)
    print "Yay! You are $%s NZ under your budget!" % (difference_under)
Reply
#5
can you post your full code, we need runnable snippet
Also show us the values to reproduce the problem
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
If I make the values of the inputs such that total_cost_car_gas is greater than budget_car_gas (For example if the inputs are 11, 40, 2.99, 800, 200, 30, 20, 5), I still get the result that I would if total_cost_car_gas was less than budget_car_gas. Here is the full code:

#Know the following things for helpful travel distances/gasprices:
#Kilometers per Liter of the car
#How many liters of petrol the car can hold
#Price of petrol
#How far you want to travel in KILOMETERS

print "Hello Eoin!"

#Definitions
def distance_with_full_tank(kpl, liters_in_the_tank):
  distance_in_kilometers = float(kpl) * float(liters_in_the_tank)
  miles = kilometers_to_miles_converter(distance_in_kilometers)
  return miles

def kilometers_to_miles_converter(distance_in_kilometers):
  miles = distance_in_kilometers/1.609
  return miles

def miles_to_kilometers_converter(miles_with_full_tank):
  distance_in_kilometers = float(kpl) * float(liters_in_the_tank)
  miles_with_full_tank = distance_in_kilometers/1.609
  km = miles_with_full_tank*1.609
  return km

def cost_of_full_tank(price_of_gas):
  cost = float(price_of_gas) * float(liters_in_the_tank)  
  return cost

def cost_of_gas(distance_travelling):
  liters_used = float(distance_travelling) / float(kpl)
  gascost = float(liters_used) * float(price_of_gas)
  return gascost

def bool_one(budget_car_and_gas):
  total_cost_car_gas = cost_of_car_1(days_car_1) + cost_of_gas(distance_travelling)
  if total_cost_car_gas > budget_car_and_gas :
    difference_over = float(total_cost_car_gas) - float(budget_car_and_gas)
    print "It seems you are $%s NZ over your budget." % (difference_over)
  elif total_cost_car_gas == budget_car_and_gas :
    print "Whew! You are exactly on your budget!"
  elif total_cost_car_gas < budget_car_and_gas :
    difference_under = float(budget_car_and_gas) - float(total_cost_car_gas)
    print "Yay! You are $%s NZ under your budget!" % (difference_under)

def cost_of_car_1(days_car_1):
  car1cost = float(price_of_car_1) + float(price_of_car_1_per_day) * float(days_car_1)
  return car1cost

def total_cost_car_gas():
  return cost_of_car_1(days_car_1) + cost_of_gas(distance_travelling)


#Declarations
kpl = raw_input("How many kilometers per liter does your car get?") #Kilometers per liter of the car that we are driving (FILL IN)
liters_in_the_tank = raw_input("How many liters of petrol can your tank hold?")
price_of_gas = raw_input("What is the price of gas per liter?")
distance_travelling = raw_input("How far do you plan on travelling in kilometers?")
budget_car_and_gas = raw_input("What is your budget for your car and gas?")
price_of_car_1 = raw_input("What is the upfront price of the car?")
price_of_car_1_per_day = raw_input("What is the price of the car per day?")
days_car_1 = raw_input("How many days will you have the car?")
distance_in_kilometers = float(kpl) * float(liters_in_the_tank)
miles_with_full_tank = distance_in_kilometers/1.609

#Main Output
print """

FULL TANK: %s miles, $%s NZ. 
That's %s kilometers with one tank of gas!

Given %s kilometers:
Gas- $%s NZ. 
Car 1- $%s NZ.
THE TOTAL COST FOR CAR AND GAS (given the distance, k/l, and prices that you specified) IS: $%s NZ.""" % (distance_with_full_tank(kpl, liters_in_the_tank), cost_of_full_tank(price_of_gas), miles_to_kilometers_converter(miles_with_full_tank), distance_travelling, cost_of_gas(distance_travelling), cost_of_car_1(days_car_1), total_cost_car_gas()) 

bool_one(budget_car_and_gas)
Reply
#7
you are comparing a float (i.e. total_cost_car_gas) with a str (i.e. budget_car_and_gas)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020