ループを終了させるために関数を取得しようとしていますが、復帰句に達した時点でループを終了しようとしていますが、失敗します。 ダイレクトコード編集ではなく、説明がうまくいきます。条件が満たされるとループは終了しません
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()
これはプログラム全体です。役立ちますように!
'UserMenu'は' UserMenu = True'に設定されているため永遠に '(" U "、" E "、" Q ")と等しくありません。誰かがあなたのプログラムとやりとりするために 'input'(python 3)か' raw_input'(python 2)を使う必要があります – roganjosh
あなたの編集はこれをより複雑にしました。 'print menu()'には何をしようとしていますか? – roganjosh
また、文字列の比較を行うときは、 'is'の代わりに' == 'を使います。それらは違う。 – Lafexlos