loop fails to terminate once conditions are met

时间:2016-12-09 13:08:07

标签: python loops

I am trying to get my function to end the loop once it hit the return clause but it fails to do so. Explanations rather than direct code editing would be appreciated.

def Menu():
    UserMenu = True
    print (""" 
        U.Create a Username
        E.Run Exponential Calculator
        Q.Exit/Quit 
        """)
    while UserMenu not in ("U", "E", "Q"):
        print("\n Error: Choice must be U, E or Q")
    return UserMenu

# Function designed to retrieve first name only from fullname entry.
def get_first_name(name):
    first=[""]
    i = 0 
    while i < len(name) and name[i] !=" ":
        first += name[i]
        i += 1
    return name[:i]

# Function designed to retrieve first initial of last name or first initial of first name if only one name input.
def get_last_initial(name):
    j = len(name) - 1 
    while j >= 0 and name[j] !=" ":
        j-=1
    return name[j+1]


# Function that generates username based upon user input.
def get_username():
    name = raw_input("Please enter your Full Name: ") 
    username = get_first_name(name) + get_last_initial(name) 
    return username.lower()



# Function to generate exponential numbers based upon usser input.
def print_exponential(): 
    base = int(raw_input("Please select a base number: \n")) 
    power = int(raw_input("Please select a power number: \n")) 
    exponential = 1 
    while power>0: 
        exponential = exponential * base 
        print base 
        if power >1:
            print "*",
        power = power -1 
    return "=%d" % exponential

print Menu()
while UserMenu != "Q":
    if UserMenu is "U":
        UserMenu = raw_input("Please enter your Full Name: ")
        print "your username is %s" % get_username()
    else:
        print print_exponential()
    print Menu()

This is the whole program, hope it helps!

2 个答案:

答案 0 :(得分:3)

你需要在循环中更新UserMenu的值,否则进入循环本身就是一个无限循环:

def Menu():
    UserMenu = raw_input(""" 
        U.Create a Username
        E.Run Exponential Calculator
        Q.Exit/Quit 
        """)
    while UserMenu not in ("U", "E", "Q"):
        UserMenu = raw_input("\n Error: Please input only U, E or Q:")
    return UserMenu


...
all your other functions
...

user_choice = Menu()
while user_choice != "Q":
    if user_choice == "U":
        print "your username is %s" % get_username()
    else:
        print_exponential()
    user_choice = Menu()

通过在循环中获取新输入,它将能够满足控制循环的条件。您编写的循环只会打印,然后再次检查UserMenu而不更改它,因此循环将永远不会退出。

答案 1 :(得分:1)

使用以下代码管理我的问题。

def Menu():
    result = raw_input (""" 
        U.Create a Username
        E.Run Exponential Calculator
        Q.Exit/Quit 
        """).upper()
    while result not in ("U", "E", "Q"):
        print("\n Error: Please input only U, E or Q:")
        result = raw_input (""" 
        U.Create a Username
        E.Run Exponential Calculator
        Q.Exit/Quit 
        """).upper()
    return result


# Function designed to retrieve first name only from fullname entry.
def get_first_name(full_name):
    i = 0 
    while i < len(full_name) and full_name[i] !=" ":
        i += 1
    return full_name[:i]

# Function designed to retrieve first initial of last name or first initial of first name if only one name input.
def get_last_initial(full_name):
    j = len(full_name) - 1 
    while j >= 0 and full_name[j] !=" ":
        j-=1
    return full_name[j+1]


# Function that generates username based upon user input.
def get_username():
    username = get_first_name(full_name) + get_last_initial(full_name) 
    return username.lower()



# Function to generate exponential numbers based upon user input.
def print_exponential(): 
    base = int(raw_input("Please select a base number: \n")) 
    power = int(raw_input("Please select a power number: \n")) 
    exponential = 1 
    while power>0: 
        exponential = exponential * base 
        print base 
        if power >1:
            print "*",
        power = power -1 
    return "=%d" % exponential

choice = Menu()
while choice != "Q":
    if choice == "U":
        full_name = raw_input("Please enter your Full Name:")
        print "your username is %s" % get_username()
    else:
        print print_exponential()
    choice = Menu()