stackedtoohigh
stackedtoohigh

Reputation: 11

While Loops Inside of Else Statement

I am new to Python and I am trying to write a while loop in an else statement so that my input statement will repeat when the user types in an option other than what is provided. If I type the correct option the first time it will skip the loop and go to the next input question. However, I cannot get the loop to end once if I type an incorrect option in the first time, but then type the correct option in on the second time. It just keeps asking the input question and will not leave the loop. It doesn't continuously print the input question until I terminate the program. It will print the input question once for each time that I enter a correct option after one incorrect option. I've tried several things and cannot get out of the loop. Any help will be greatly appreciated and thank you in advanced.

from PIL import Image



image_beach = Image.open("beach.jpg")
image_desert = Image.open("desert.jpg")
image_field = Image.open("field.jpg")
image_forest = Image.open("forest.jpg")
image_snowscape = Image.open("snowscape.jpg")
image_sunset = Image.open("sunset.jpg")
image_earth = Image.open("earth.jpg")
image_boat = Image.open("boat.jpg")
image_cactus = Image.open("cactus.jpg")
image_cat_small = Image.open("cat_small.jpg")
image_cat = Image.open("cat.jpg")
image_harvester = Image.open("harvester.jpg")
image_hiker = Image.open("hiker.jpg")
image_penguin = Image.open("penguin.jpg")
image_shuttle = Image.open("spaceshuttle.jpg")

#background_image = image_beach or image_desert or image_earth or image_field or image_forest or image_snowscape or image_sunset

#foreground_image = image_boat or image_cactus or image_cat_small or image_cat or image_harvester or image_hiker or image_penguin or image_shuttle

first_choice = input(f"\nPlease select a background image from the following list. (BEACH, DESERT, EARTH, FIELD, FOREST, SNOWSCAPE, SUNSET): ").lower().strip()

background_image = first_choice


if first_choice == "beach":
    first_choice = image_beach
elif first_choice == "desert":
    first_choice = image_desert
elif first_choice == "earth":
    first_choice = image_earth
elif first_choice == "field":
    first_choice = image_field
elif first_choice == "forest":
    first_choice = image_forest
elif first_choice == "snowscape":
    first_choice = image_snowscape
elif first_choice == "sunset":
    first_choice = image_sunset
else:
    while first_choice != "beach" or "desert" or "earth" or "field" or "forest" or "snowscape" or "sunset":
        print("\nInvalid Option")

        
        first_choice = input(f"\nPlease select a background image from the following list. (BEACH, DESERT, EARTH, FIELD, FOREST, SNOWSCAPE, SUNSET): ").lower().strip()
   
    
        if first_choice.lower().strip == "beach" or "desert" or "earth" or "field" or "forest" or "snowscape" or "sunset":
           
               
        first_choice == background_image
    #background_image == first_choice
        
        if first_choice == background_image:
                background_image = image_beach or image_desert or image_earth or image_field or image_forest or image_snowscape or image_sunset
        
        
        
        
        
        
                if background_image == image_beach:
                    background_image = Image.open("beach.jpg")
                elif background_image == image_desert:
                    background_image = Image.open("desert.jpg")
                elif background_image == image_earth:
                    background_image = Image.open("earth.jpg")
                elif background_image == image_field:
                    background_image = Image.open("field.jpg")
                elif background_image == image_forest:
                    background_image = Image.open("forest.jpg")
                elif background_image == image_snowscape:
                    background_image = Image.open("snowscape.jpg")
                elif background_image == image_sunset:
                    background_image = Image.open("sunset.jpg")



second_choice = input(f"\nPlease select a foreground image from the following list. (BOAT, CACTUS, SMALL CAT, BIG CAT, HARVESTER, HIKER, PENGUIN, SPACE SHUTTLE): ").lower().strip()
if second_choice == "boat":
    second_choice = image_boat
elif second_choice == "cactus":
    second_choice = image_cactus
elif second_choice == "small cat":
    second_choice = image_cat_small
elif second_choice == " big cat":
    second_choice = image_cat
elif second_choice == "harvester":
    second_choice = image_harvester
elif second_choice == "hiker":
    second_choice = image_hiker
elif second_choice == "penguin":
    second_choice = image_penguin
elif second_choice == "space shuttle":
    second_choice = image_shuttle
else:
    while second_choice.lower().strip() != "boat" or "cactus" or "small cat" or "cat" or 
"harvester" or "hiker" or "penguin" or "space shuttle":
        print("\nInvalid Option")
        second_choice = input(f"\nPlease select a foreground image from the following list. (BOAT, CACTUS, SMALL CAT, BIG CAT, HARVESTER, HIKER, PENGUIN, SPACE SHUTTLE): ").lower().strip()
        if second_choice.lower().strip() == "boat" or "cactus" or "small cat" or "cat" or "harvester" or "hiker" or "penguin" or "space shuttle":
        
            foreground_image == second_choice
  #  foreground_image == second_choice
            #break
        if second_choice == foreground_image:
            foreground_image = image_boat or image_cactus or image_cat_small or image_cat or image_harvester or image_hiker or image_penguin or image_shuttle

            if foreground_image == image_boat:
                foreground_image = Image.open("boat.jpg")
            elif foreground_image == image_cactus:
                foreground_image = Image.open("cactus.jpg")
            elif foreground_image == image_cat_small:
                foreground_image = Image.open("cat_small.jpg")
            elif foreground_image == image_cat:
                foreground_image = Image.open("cat.jpg")
            elif foreground_image == image_harvester:
                foreground_image = Image.open("harvester.jpg")
            elif foreground_image == image_hiker:
                foreground_image = Image.open("hiker.jpg")
            elif foreground_image == image_penguin:
                foreground_image = Image.open("penguin.jpg")
            elif foreground_image == image_shuttle:
                foreground_image = Image.open("spaceshuttle.jpg")

background_image = first_choice

foreground_image = second_choice

combined_image = Image.new("RGB", background_image.size)
background_pixels = background_image.load()
foreground_pixels = foreground_image.load()
pixels_combined = combined_image.load()
width, height = background_image.size





for x in range(0, width):
    for y in range(0, height):
        (r, g, b) = foreground_pixels[x, y]




        if r < 100 and g > 110 and b < 130:
            foreground_pixels[x, y] = background_pixels[x, y]


        #if r < 90 and g > 99 and b < 255:
        #    foreground_pixels[x, y] = background_pixels[x, y]

        else:
            foreground_pixels[x, y] = foreground_pixels[x, y]


    



        pixels_combined[x, y] = foreground_pixels[x, y]

#print(pixels_combined[381, 198])


combined_image = Image.blend(foreground_image,background_image,0.5)
combined_image.save("earthling_shuttler.jpg")



combined_image.show()

Upvotes: 0

Views: 71

Answers (1)

saquintes
saquintes

Reputation: 1089

This line your problem.

while first_choice != "beach" or "desert" or "earth" or "field" or "forest" or "snowscape" or "sunset":

The loop will never end because you are checking whether first_choice is equal to "beach", but then checking whether "desert" is True, which is always will be.

Try:

while first_choice not in ["beach", "desert", "earth", "field", "forest", "snowscape", "sunset"]:

You'll need to do the same thing to if statement where you make the same erroneous check.

-- UPDATE --

There is a lot of repetition in your code. I haven't execute this, but it's a suggestion for some refactoring to make it a bit simplier.


from PIL import Image

images = ['beach', 'desert', 'field', 'forest', 'snowscape', 'sunset', 'earth', 'boat', 'cactus', 'cat_small', 'cat', 'harvester', 'hiker', 'penguin', 'spaceshuttle']

image_map = {
    name: Image.open("{}.jpg".format(name))
    for name in images
}

first_choice = None

while first_choice is None:
    first_choice = input(f"\nPlease select a background image from the following list. ({}): ".format(', '.join(images))).lower().strip()
    if first_choice not in images:
        print("Invalid choice")
        first_choice = None
        
background_image = image_map[first_choice]

second_choice = None

while second_choice is None:
    second_choice = input(f"\nPlease select a foreground image from the following list. ({}): ".format(', '.join(images))).lower().strip()
    if second_choice not in images:
        print("Invalid choice")
        second_choice = None
        
foreground_image = image_map[second_choice]

combined_image = Image.new("RGB", background_image.size)
background_pixels = background_image.load()
foreground_pixels = foreground_image.load()
pixels_combined = combined_image.load()
width, height = background_image.size

for x in range(0, width):
    for y in range(0, height):
        (r, g, b) = foreground_pixels[x, y]

        if r < 100 and g > 110 and b < 130:
            foreground_pixels[x, y] = background_pixels[x, y]


        # if r < 90 and g > 99 and b < 255:
        #    foreground_pixels[x, y] = background_pixels[x, y]

        else:
            foreground_pixels[x, y] = foreground_pixels[x, y]

        pixels_combined[x, y] = foreground_pixels[x, y]

# print(pixels_combined[381, 198])


combined_image = Image.blend(foreground_image, background_image, 0.5)
combined_image.save("earthling_shuttler.jpg")

combined_image.show()

Upvotes: 2

Related Questions