2016-11-27 11 views
-1
counter=0 
initials=0 
name1=raw_input("Please enter your first name!") 
name2=raw_input("Please enter your middle name!") 
name3=raw_input("Please enter your last name!") 
option=str(raw_input("a)Print the length of my name\nb)Print\nc)Exit\nChoose one of the options.")) 
while option != "c" or option != "C": 
    if (option=="a" or option=="A"): 
     print "Your first name has " + str(len(name1)) + " letters." 
     print "Your second name has " + str(len(name2)) + " letters." 
     print "Your last name has " + str(len(name3)) + " letters." 
    elif (option=="b" or option=="B"): 
     print name1[0] + "." + name2[0] + "." + name3[0] 
    elif (option=="c" or option=="C"): 
     break 

これは私のコードです。それは何らかの理由で無限ループに入り続けます。ユーザーがオプションを選択すると、それを停止させるにはどうすればよいですか?ユーザーが一度選択したオプションの入力が必要です。Pythonプログラミング無意識の無限ループ

+2

コードがインデントされているようです。チェックしてください。 – usr2564301

答えて

5

あなたは、whileループの終わりにライン

option=str(raw_input("a)Print the length of my name\nb)Print\nc)Exit\nChoose one of the options.")) 

を追加する必要があります。これは、ユーザーに再度入力を要求し、cまたはCの場合は終了します。

whileの状態も変更する必要があります。 orを使用すると、常にFalseになります。おそらくwhile Trueを使用するべきです(ループ内でc入力がチェックされているため)。

4

希望する場合は、条件をTrueに変更して、オプションの明細書の位置を変更する必要があります。

counter=0 
initials=0 
name1=raw_input("Please enter your first name!") 
name2=raw_input("Please enter your middle name!") 
name3=raw_input("Please enter your last name!") 

while True: 
    option=str(raw_input("a)Print the length of my name\nb)Print\nc)Exit\nChoose one of the options.")) 
    if (option=="a" or option=="A"): 
     print "Your first name has " + str(len(name1)) + " letters." 
     print "Your second name has " + str(len(name2)) + " letters." 
     print "Your last name has " + str(len(name3)) + " letters." 
    elif (option=="b" or option=="B"): 
     print name1[0] + "." + name2[0] + "." + name3[0] 
    elif (option=="c" or option=="C"): 
     break 
+0

'option ==" c "やoption ==" C "'などの入力を避けるため、変数 'option'を' option = str(raw_input( "x"))として設定することができます。 ()はこの[要点](https://gist.github.com/rrrub/ee4257a737c3e8f8ca42b8c9bfa5a97b)に示されているように – rrrub

+0

@gist - rrrubはいそうです。これも可能です... 'option = str(raw_input ( "a)私の名前の長さを印字する\ nb)Print \ nc)終了\ nオプションを選ぶ"))lower() ' これ以降は、 'letter(オプション==" C "') –