Reputation: 77
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!
Upvotes: 1
Views: 90
Reputation: 77
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()
Upvotes: 1
Reputation: 4469
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.
Upvotes: 3