Hey guys

Andrew here, with another problem.

My while loop is made so that it is on, or off, simple eh? That's what I thought, but it doesn't seem to be acting as I would expect it to!

The problem is, when I want it to loop, in testing it didn't loop, which is a problem!

This is my code:

print "HOW MANY VARIABLES WOULD YOU LIKE?"
print "2"
print "3"
print "4"
print "5"
print "6"
print "7"
print "8"
print "9"
print "10"
print "--------------------------------------------------------------------------------"
MainLoop = 1
while MainLoop == 1:
    variables = raw_input (" ")
    if variables == "2":
        name1 = raw_input ("NAME YOUR FIRST VARIABLE ")
        time.sleep(1)
        name2 = raw_input ("NAME YOUR SECOMD VARIABLE ")
        time.sleep(1)
        print "--------------------------------------------------------------------------------"
        NameInHat = raw_input ("PLEASE PRESS ENTER TO PICK YOUR RANDOM VARIABLE ")
        loop = 1
        while loop == 1:
            randomg = random.randint(1, 2)
            if randomg == 1:
                print name1
            elif randomg == 2:
                print name2

            end = raw_input ("WOULD YOU LIKE TO FINISH? ")
            if end == "YES" or "yes" or "y" or "Y":
                loop = 2
            elif end == "no" or "NO" or "n" or "N":
                print " "
        print "THANK YOU USING MY APP."
        menu = raw_input ("WOULD YOU LIKE TO GO BACK TO THE MENU? ")
        if menu == "no" or "NO" or "n" or "N":
            MainLoop = 2
            exit
        elif menu == "YES" or "yes" or "y" or "Y":
            print " "

Hope this is fixable. And could someone also provide an explanation as to why it isn't working, and how they fixed it!

Dani AI

Generated

Quick summary and fix: the inner loop is ending immediately because the condition if end == "YES" or "yes" or "y" or "Y": does not do what you expect — Python evaluates that as (end == "YES") or "yes" or ... and non-empty strings are truthy, so the branch always runs. Also exit by itself doesn't quit the script; call sys.exit() or use break/loop control. Normalize user input (.strip().lower()) and use membership tests or startswith for yes/no checks. Use a list for variable names and random.choice() instead of repeated numbered variables.

A compact, Python2/3-friendly pattern you can use:

# Python 2 & 3 compatible
try:
    input = raw_input
except NameError:
    pass

import random, sys, time

def choose_name(names):
    while True:
        print(random.choice(names))
        ans = input("WOULD YOU LIKE TO FINISH? ").strip().lower()
        if ans in ("yes", "y"):
            break

def main_menu():
    while True:
        n = input("HOW MANY VARIABLES WOULD YOU LIKE? (2-10) ").strip()
        if not n.isdigit() or not (2 <= int(n) <= 10):
            print("Enter a number between 2 and 10.")
            continue
        names = [input("NAME YOUR VARIABLE #{}: ".format(i+1)).strip() for i in range(int(n))]
        input("PRESS ENTER TO PICK YOUR RANDOM VARIABLE")
        choose_name(names)
        again = input("WOULD YOU LIKE TO GO BACK TO THE MENU? ").strip().lower()
        if again in ("no", "n"):
            sys.exit(0)

if __name__ == "__main__":
    main_menu()

Notes and troubleshooting tips: reference and normalize inputs early (use .strip().lower()), validate numeric input with isdigit() or try/except int(), and prefer random.choice(list) for readability. As hinted, make sure your outer-loop control actually changes on all paths. As pointed out, chained or with literals causes logic bugs. 's suggestion to use while True plus break is a clean way to structure this flow. If something still misbehaves, print repr(value) to inspect trailing whitespace or unexpected characters.

What happens when "variables != '2'" ?

## Three variables
print "HOW MANY VARIABLES WOULD YOU LIKE?"
for ctr in range(3):
    print ctr+1

names_list=["FIRST", "SECOND", "THIRD"]
MainLoop = 1
while MainLoop == 1:
    name_of_variables=[]
    variables = raw_input ("Enter Variables ")
    for ctr in range(int(variables)):
        name = raw_input ("NAME YOUR %s VARIABLE " % (names_list[ctr]))
        name_of_variables.append(name)

    ## exit testing while() loop
    MainLoop = 0

print name_of_variables

Oh I have all of that, it is essentially, variable 1 copied, and adding a third possible variable!

The point is MainLoop is not changed if variables does not equal 2.

also

  anything or 'no'

is always True as 'no' is not empty string.

instead of confusing while MainLoop == 1 you can use:

while True:
    '''do stuff'''
    break
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.