2016-12-09 10 views
1

ループを終了させるために関数を取得しようとしていますが、復帰句に達した時点でループを終了しようとしていますが、失敗します。 ダイレクトコード編集ではなく、説明がうまくいきます。条件が満たされるとループは終了しません

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

これはプログラム全体です。役立ちますように!

+4

'UserMenu'は' UserMenu = True'に設定されているため永遠に '(" U "、" E "、" Q ")と等しくありません。誰かがあなたのプログラムとやりとりするために 'input'(python 3)か' raw_input'(python 2)を使う必要があります – roganjosh

+0

あなたの編集はこれをより複雑にしました。 'print menu()'には何をしようとしていますか? – roganjosh

+0

また、文字列の比較を行うときは、 'is'の代わりに' == 'を使います。それらは違う。 – Lafexlos

答えて

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

ループ内のメッセージを貼り付けてコピーしなかった方が、はるかに良いでしょう。しかし、あなたは答えがまだ間違っています。 –

+0

whoops、 '!='を 'not in'に変更するのを忘れました。要点は、制御条件をループ内で更新する必要があることです。 – Will

+0

ここでは、ここで何がうまくいかないのかちょっと解説しておけばいいと思います。 OPには6つの答えがあった。実際には、 'print'は視覚的なものなので、入力を促す必要があります。また、これがPython 2の場合、期待通りに動作しません。 – roganjosh

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() 
関連する問題