Day 015 - Project 15 : All About the Loop

If you have any questions, comments or issues with this project please post them here!

I dont know how to add the while loop in this code. I added it at the beginning but it kept repeating endlessly and I don’t know how to define the end of the loop. I wrote the code below.

The request is “Write a program that loops. Inside the loop, ask the user what animal sound they want to hear”
What animal do you want?: Cow
A cow goes moo.
Do you want to exit?: no
What animal do you want?: A Lesser Spotted lemur
Ummm…the Lesser Spotter Lemur goes awooga.
Do you want to exit?: yes

Code
animal = input("What animal sounds do you want to hear? ")
if animal == “cow”:
print(“A cow goes moo”)
elif animal == “lemur”:
print(“Ummm…the lemur goes awooga”)
else:
print(“Try cow or lemur”)
exit = input(“do you want to exit?”)

while True:
    animal = input("What animal sounds do you want to hear? ")
    if animal == "cow":
        print("A cow goes moo")
    elif animal == "lemur":
        print("Ummm…the lemur goes awooga")
    else:
        print("Try cow or lemur")
    exit = input("do you want to exit?")
    if exit == "yes":
        break
    else:
        continue

break will exit the while loop and continue will continue the loop

2 Likes

Use the break keyword to break from the while loop.

if exit == "exit":
  break
1 Like

Thank you so much for the quick reply!

1 Like

Just moved your post and replies to Day 015 topic so others can find it if needed!

3 Likes

What does the :fox_face: say ? Find out with my custom animal sound generator (just like Alexa)!

https://replit.com/@JackAdem/Day-015-Project-15-All-About-the-Loop?v=1

Day 15 of #Replit100DaysOfCode #100DaysOfCode.

1 Like

Don’t understand why “shut_machine” global variable doesn’t change when it’s supposed to:

MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}

shut_machine = False

def coffee_machine():
    resources["Money"] = 0.0
    #  TODO 1. prompt "report" def report: to tell you the remaining resources in the machine
    order = input("what would you like? (espresso/latte/cappuccino):")

    #  TODO 2. prompt "what would you like? (espresso/latte/cappuccino):" def check: to check if the remaining sources
    #   are sufficient, and return what's missing or allow proceed coffee making
    def check(coffee):
        if order != "report":
            for i in MENU[coffee]["ingredients"].keys():
                resources[i] -= MENU[coffee]["ingredients"][i]
                if resources[i] < 0:
                    print(f"add {i}")
                    global shut_machine
                    shut_machine = True
        if coffee == "report":
            print(resources)
    check(order)

    #  TODO 3. ask for payment, then refund or give changes
    global shut_machine
    if not shut_machine:
        paying = input("please insert coins: quarters, dimes, nickles, pennies\n")
        n_q = int(paying.split(",")[0])
        n_d = int(paying.split(",")[1])
        n_n = int(paying.split(",")[2])
        n_p = int(paying.split(",")[3])

        def coins_total():
            return n_q * 0.25 + n_d * 0.1 + n_n * 0.05 + n_p * 0.01

        # TODO 4. update resources:
        if coins_total() >= MENU[order]["cost"]:
            change = coins_total() - MENU[order]["cost"]
            print(f"{order} is coming right up!, and your change is {round(change, 2)}")
            resources["Money"] += (coins_total() - change)
        else:
            print(f"your money is not enough, and you are refunded")
            return
    while not shut_machine:
        coffee_machine()


coffee_machine()

Is resources declared also or somehow implicit (from an imported file) in the exercise you are doing?
I only see an assignment but no declaration.

Btw it is always better to avoid global variables …

2 Likes

Absolutely agree, Global variable is better not to be messed

I struggled with global variables the last time I needed them, so I cheated and made a text file. Now I have my own commands in my own library (pip install CoderElijah) to use text files for global variables if I need to. That, of course, is for worst-case scenarios only.

Question:

exit = "no"
while exit == "no":
  animal_sound = input("What animal sound do you want to hear?")
  
  if animal_sound == "cow" or animal_sound == "Cow":
    print("🐮 Moo")
  elif animal_sound == "pig" or animal_sound == "Pig":
    print ("🐷 Oink")
  elif animal_sound == "sheep" or animal_sound == "Sheep":
    print ("🐑 Baaa")
  elif animal_sound == "duck" or animal_sound == "Duck":
    print("🦆 Quack")
  elif animal_sound == "dog" or animal_sound == "Dog":
    print("🐶 Woof")
  elif animal_sound == "cat" or animal_sound == "Cat":
    print("🐱 Meow")
  else: 
    print("I don't know that animal sound. Try again.")

I tried this day15 code challenge but the loop was repeating and it was not exiting and I also looked the solution but it was not working.

exit = “no”
while exit == “no”:
  animal_sound = input(“What animal sound do you want to hear?”)

  if animal_sound == “cow” or animal_sound == “Cow”:
    print(“:cow: Moo”)
  elif animal_sound == “pig” or animal_sound == “Pig”:
    print (“:pig: Oink”)
  elif animal_sound == “sheep” or animal_sound == “Sheep”:
    print (“:sheep: Baaa”)
  elif animal_sound == “duck” or animal_sound == “Duck”:
    print(“:duck: Quack”)
  elif animal_sound == “dog” or animal_sound == “Dog”:
    print(“:dog: Woof”)
  elif animal_sound == “cat” or animal_sound == “Cat”:
    print(“:cat: Meow”)
  else:
    print(“I don’t know that animal sound. Try again.”)

Just some formatting. As for the question, until exit value is nit changed, the loop will never end …

4 Likes


see actually I typed your code but when I run it ,it is not asking if I want to exit? instead it is asking the same question what animal sound do you want to hear?

Then at the end of the cycle, ask the user if he wants to continue:

exit = input("Stop the program? ")


Look I did that already

Your code is not working properly due to the fact that you did not indent after line number 5. The conditions must be inside the while loop

2 Likes

can you show where I made wrong actually tried but still not getting???

Add one indentation per lines 7 to 20.
image

3 Likes

Hello, everyone! :smiling_face:

Total novice here. I would really love it if someone could help me figure out why the output says “Wrong answer! Try again. [next line] Correct answer!” instead of just “Correct answer!” when I put in “river”.

Thanks in advance!