Oliver Warrington
Oliver Warrington

Reputation: 3

Check after random sample in python

This is my first little project and first question so sorry for the formatting. I have two lists containing recipe names as strings. I want to ask the user how many meals from each list they want and then take a random selection.

I then want to check if a meal was chosen by the selection and display the price of the meal which I would store in a variable.

My idea was to add the random sample to a new list and to check the contents via an if in statement. If the recipe was inside the new list then it would print the variable containing the price.

When I check the new list for the recipe however, it doesn't think it is in the list. Is there something I've missed or a better way to do this? Thank you.

My code so far:

import random
Vegetarian = ["Jungle Curry", "Chickpea Curry", "Dhal", "Buddha Bowl", 
"Chickpea Salad", "Lazy Noodles", "Fry Up"]
Meat = ["Chilli", "Butter Chicken", "Lamb and Hummus"]

v = int(raw_input("How many vegetarian meals this week? > "))
m = int(raw_input("How many meaty meals? > "))

Veg1 = random.sample(Vegetarian, v)
Meat2 = random.sample(Meat, m)

veg_week = []
meat_week = []

veg_week.append(Veg1)
meat_week.append(Meat2)

jungle_curry = 10
chickpea_curry = 10

if "Jungle Curry" and "Chickpea Curry" in veg_week:
    print jungle_curry + chickpea_curry

Upvotes: 0

Views: 110

Answers (2)

Israel-abebe
Israel-abebe

Reputation: 578

i would have done it like this

import random

Vegetarian = ["Jungle Curry", "Chickpea Curry", "Dhal", "Buddha Bowl",
          "Chickpea Salad", "Lazy Noodles", "Fry Up"]
Meat = ["Chilli", "Butter Chicken", "Lamb and Hummus"]

v = int(raw_input("How many vegetarian meals this week? > "))
m = int(raw_input("How many meaty meals? > "))

Veg1 = random.sample(Vegetarian, v)
Meat2 = random.sample(Meat, m)

veg_week = []
meat_week = []

veg_week.append(Veg1)
meat_week.append(Meat2)

# put the prices of each in the list too 
Vegetarian_price = [10, 10, 10, 10, 10, 10, 10]
Meat_price = [20, 20, 20]

v_cost = 0
m_cost = 0
for i in range(len(Veg1)):
    v_cost += Vegetarian_price[Vegetarian.index(Veg1[i])]

for i in range(len(Meat2)):
    m_cost += Meat_price[Meat.index(Meat2[i])]

print v_cost,m_cost

Upvotes: 0

Reblochon Masque
Reblochon Masque

Reputation: 36682

You have several mistakes, let's go through them:

  • The result of random.sample is a list; when you call veg_week.append(Veg1', you are creating a list of lists.
  • After this, you are assigning the value 10 to jungle_curry and chickpea_curry
  • Then, you are checking if both "Jungle Curry" and "Chickpea Curry" are in the selected sample; maybe you want to check if these are in this week's menu one by one?

This should work better:

import random

Vegetarian = ["Jungle Curry", "Chickpea Curry", "Dhal", "Buddha Bowl", 
"Chickpea Salad", "Lazy Noodles", "Fry Up"]
Meat = ["Chilli", "Butter Chicken", "Lamb and Hummus"]

v = int(input("How many vegetarian meals this week? > "))
m = int(input("How many meaty meals? > "))

Veg1 = random.sample(Vegetarian, v)
Meat2 = random.sample(Meat, m)

if "Jungle Curry" and "Chickpea Curry" in veg1:
    print(jungle_curry + chickpea_curry)

Upvotes: 1

Related Questions