1

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!

  • 4
    `UserMenu` will forever not be equal to `("U", "E", "Q")` because you set it as `UserMenu = True`. You need to use `input` (python 3) or `raw_input` (python 2) for someone to interact with your program – roganjosh Dec 09 '16 at 13:10
  • Your edit has made this more complicated. What are you intending for `print Menu()`? – roganjosh Dec 09 '16 at 13:22
  • Also, when doing string comparison, use `==` instead of `is`. They are different. – Lafexlos Dec 09 '16 at 13:24

2 Answers2

3

You need to update the value of UserMenu inside the loop, or else entering the loop will inherently be an infinite loop:

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()

By getting new input in the loop, it will be able to meet the criteria that controls the loop. The loop you wrote will just print then check UserMenu again without changing it, so the loop will never exit.

Will
  • 4,299
  • 5
  • 32
  • 50
  • 1
    It would be sligtly better if you didn't copy pasted the message in the loop. But you're answer is still wrong. – Loïc Faure-Lacroix Dec 09 '16 at 13:14
  • whoops, forgot to change `!=` to `not in`, the code should work now. The point is that the control condition needs to be updated within the loop. – Will Dec 09 '16 at 13:19
  • At this point I think it would be good if you could expand a bit on what went wrong here. The OP had 6 (I think) answers that all missed the mark. The fact is that `print` is visual only so they need to prompt for input. Also, if this is Python 2 then it will not behave as expected. – roganjosh Dec 09 '16 at 13:20
  • And, looking at the last edit on question, this is indeed python 2. – Lafexlos Dec 09 '16 at 13:21
  • @Will thanks for the info, that has actually helped me understand the concept a bit better. However now that I have made your changes all my input choices are coming up not defined! – Darren Macis Dec 09 '16 at 13:35
  • Where is the explanation for `raw_input`? You now have a mix. @DarrenMacis swap `input` for `raw_input` in all cases. – roganjosh Dec 09 '16 at 13:38
  • @DarrenMacis I edited it again to correct the bottom of the script where you're calling `Menu` and your other functions. If you're still having issues or need more explanation, open a new question. – Will Dec 09 '16 at 13:42
  • @roganjosh thanks for that, I think I've got it all in python 2 now – Will Dec 09 '16 at 13:44
  • @Will I am a bit confused as to the purpose of the raw_input for the UserMenu = raw_input("\n Error: Please input only U, E or Q:"). all I want to do there is print the error message and then prompt them for another input after. – Darren Macis Dec 09 '16 at 13:52
  • `raw_input("some message")` will print `some message` (or whetever you put in the parameter) then take input returned as a string. as for `input()` ve `raw_input`, see here: http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x – Will Dec 09 '16 at 13:54
1

Managed to sort out my problem using the following code.

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()