Home > Software engineering >  How to Connect User input to While Loop
How to Connect User input to While Loop

Time:01-19

I am making a code where the user is supposed to choose a meat to eat and then it randomly assigns other foods to go with it to complete a meal. The loop should show you what items you have and at the end of the loop should print out the total calorie value. I coded the whole program but when I prompt the user for a meat, it stops and doesn't go to the while loop. Here is the program:

import random

usermeal = []
meatcalories = 0
fruitcalories = 0
vegcalories = 0
desertcalories = 0

#defining a list of different foods...
meats = {"NY Strip": 300, "Shrimp": 100, "Lobster": 200, "Clams": 300, "Scallops": 200}
vegetables = {"Asparagus": 50, "Potato": 80, "Brocoli": 50, "Kale": 30, "Beans": 40}
fruits = {"Apple": 40, "Orange": 30, "Bananna":45, "Kiwi": 40, "Mango": 40}
deserts = {"Ice cream":300, "Brownie": 400, "Cookie": 200, "Chocolate":300, "Candy": 100}


print("We are going to make you a meal today. Each meal will have one item from each food category:")
print(meats)
print(vegetables)
print(fruits)
print(deserts)
print("Start by choosing one of the meats")
meatchoice = input("Please choose the meat that you want: ")


while len(usermeal) == 4:
    #prompt the user for their meat decision
    if meatchoice not in meats:
        print("Please choose a meat from the options!")
    meatchoice = input("Please choose the meat that you want: ")
    if meatchoice in meats:
        usermeal.append(meatchoice)
        meatcalories = meats.get(meatchoice)
        print("This is your meal so far", usermeal)

#randomly chooses a vegetable
    print("Now lets get the vegetable!")
    random_veg = random.choice(list(vegetables))
    print("The random vegetable is:", random_veg)
    usermeal.append(random_veg)
    vegcalories = vegetables.get(random_veg)
    print("This is your meal so far,", usermeal, "and its calorie count:", vegcalories)

#randomly chooses a fruit
    print("Now lets get the fruit!")
    random_fruit = random.choice(list(fruits))
    print("The random fruit is:", random_fruit)
    usermeal.append(random_fruit)
    fruitcalories = fruits.get(random_fruit)
    print("This is your meal so far,", usermeal, "and its calorie count:", fruitcalories)

#randomly chooses a desert
    print("Now lets get the desert!")
    random_desert = random.choice(list(deserts))
    print("The random desert is:", random_desert)
    usermeal.append(random_desert)
    desertcalories = deserts.get(random_desert)
    print("This is your meal so far,", usermeal, "and its calorie count:", desertcalories)

print("Now since we have all the foods, lets see how many calories it is")
totalcals = meatcalories   vegcalories   fruitcalories   desertcalories
print("Total Calorie Count: ", totalcals)

I'm not exactly sure what the problem is. I know its something really small. If anyone can point it out to me I'd really appreciate it. Show the change in code please. Thanks in advance!

CodePudding user response:

Look at what the condition is for your while loop.

while len(usermeal) == 4:

Now before the while loop runs, run this and see what it outputs

print(len(usermeal))

The TL;DR is that your conditional to run the while loop isn't true when it comes across it so the loop never runs.

CodePudding user response:

import random

usermeal = []
meatcalories = 0
fruitcalories = 0
vegcalories = 0
desertcalories = 0

#defining a list of different foods...
meats = {"NY Strip": 300, "Shrimp": 100, "Lobster": 200, "Clams": 300, "Scallops": 200}
vegetables = {"Asparagus": 50, "Potato": 80, "Brocoli": 50, "Kale": 30, "Beans": 40}
fruits = {"Apple": 40, "Orange": 30, "Bananna":45, "Kiwi": 40, "Mango": 40}
deserts = {"Ice cream":300, "Brownie": 400, "Cookie": 200, "Chocolate":300, "Candy": 100}


print("We are going to make you a meal today. Each meal will have one item from each food category:")
print(meats)
print(vegetables)
print(fruits)
print(deserts)
print("Start by choosing one of the meats")
meatchoice = input("Please choose the meat that you want: ")


while len(usermeal) != 4:  #changed condition to NOT 4
    #prompt the user for their meat decision
    if meatchoice not in meats:
        print("Please choose a meat from the options!")
        meatchoice = input("Please choose the meat that you want: ")
    elif meatchoice in meats:  # also moved meatchoice within the if, and made this an elif
        usermeal.append(meatchoice)
        meatcalories = meats.get(meatchoice)
        print("This is your meal so far", usermeal)

#randomly chooses a vegetable
    print("Now lets get the vegetable!")
    random_veg = random.choice(list(vegetables))
    print("The random vegetable is:", random_veg)
    usermeal.append(random_veg)
    vegcalories = vegetables.get(random_veg)
    print("This is your meal so far,", usermeal, "and its calorie count:", vegcalories)

#randomly chooses a fruit
    print("Now lets get the fruit!")
    random_fruit = random.choice(list(fruits))
    print("The random fruit is:", random_fruit)
    usermeal.append(random_fruit)
    fruitcalories = fruits.get(random_fruit)
    print("This is your meal so far,", usermeal, "and its calorie count:", fruitcalories)

#randomly chooses a desert
    print("Now lets get the desert!")
    random_desert = random.choice(list(deserts))
    print("The random desert is:", random_desert)
    usermeal.append(random_desert)
    desertcalories = deserts.get(random_desert)
    print("This is your meal so far,", usermeal, "and its calorie count:", desertcalories)

print("Now since we have all the foods, lets see how many calories it is")
totalcals = meatcalories   vegcalories   fruitcalories   desertcalories
print("Total Calorie Count: ", totalcals)

I also think you could move everything that is chosen randomly outside of the while and only loop the meat choice condition. And I think that you could change the condition to while meatchoice not in meats. But, it works as is.

Update:

So if you wanted to change the logic, it would be like this:

import random

usermeal = []
meatcalories = 0
fruitcalories = 0
vegcalories = 0
desertcalories = 0

# defining a list of different foods...
meats = {"NY Strip": 300, "Shrimp": 100,
         "Lobster": 200, "Clams": 300, "Scallops": 200}
vegetables = {"Asparagus": 50, "Potato": 80,
              "Brocoli": 50, "Kale": 30, "Beans": 40}
fruits = {"Apple": 40, "Orange": 30, "Bananna": 45, "Kiwi": 40, "Mango": 40}
deserts = {"Ice cream": 300, "Brownie": 400,
           "Cookie": 200, "Chocolate": 300, "Candy": 100}


print("We are going to make you a meal today. Each meal will have one item from each food category:")
print(meats)
print(vegetables)
print(fruits)
print(deserts)
print("Start by choosing one of the meats")
meatchoice = input("Please choose the meat that you want: ")


while meatchoice not in meats:
    # prompt the user for their meat decision
    print("Please choose a meat from the options!")
    meatchoice = input("Please choose the meat that you want: ")

usermeal.append(meatchoice)
meatcalories = meats.get(meatchoice)
print("This is your meal so far", usermeal)

# randomly chooses a vegetable
print("Now lets get the vegetable!")
random_veg = random.choice(list(vegetables))
print("The random vegetable is:", random_veg)
usermeal.append(random_veg)
vegcalories = vegetables.get(random_veg)
print("This is your meal so far,", usermeal,
      "and its calorie count:", vegcalories)

# randomly chooses a fruit
print("Now lets get the fruit!")
random_fruit = random.choice(list(fruits))
print("The random fruit is:", random_fruit)
usermeal.append(random_fruit)
fruitcalories = fruits.get(random_fruit)
print("This is your meal so far,", usermeal,
      "and its calorie count:", fruitcalories)

# randomly chooses a desert
print("Now lets get the desert!")
random_desert = random.choice(list(deserts))
print("The random desert is:", random_desert)
usermeal.append(random_desert)
desertcalories = deserts.get(random_desert)
print("This is your meal so far,", usermeal,
      "and its calorie count:", desertcalories)

print("Now since we have all the foods, lets see how many calories it is")
totalcals = meatcalories   vegcalories   fruitcalories   desertcalories
print("Total Calorie Count: ", totalcals)
  •  Tags:  
  • Related