2017-10-19 8 views
-1

私はPythonが初めてで、非常に特殊なループを作成しようとしています。私が読んだものは、私を本当に助けてくれました。たぶんそれは私が意味するものではない、またはおそらく私が理解していないためです。私は尋ねようとし、誰かが私を理解してくれることを祈るでしょう私は基本的にちょうど上から4行を再生しようとしています。これについてPythonでif/elseループを作成する

def defoption(): 
    option = ("Give an input") #<- Trying to replay this line 
    option_input = input("Input: ") #<- and then this 
    if (option_input == 'a'): 
     print("Option a works") 
    else: 
     return option and print("Option unavailable") 
defoption() 
+0

あなたの質問は本当に不明である...あなたは、 "リプレイ" に何をしたいですか?あなたは何を達成しようとしていますか? – Sayse

+0

期待される出力は何ですか? –

+1

多分、オプションが何かに等しくないのに対して、あなたはそのループを繰り返す必要がありますか? – rmjoia

答えて

-1

何:

def defoption(): 
    print("Give an input") #<- Trying to replay this line 
    option_input = input("Input: ") #<- and then this 
    if option_input == 'a': 
     print("Option a works") 
    else: 
     print("Option unavailable") 
     return defoption() 

defoption() 
+0

うわー、私は3時間この作業をしていましたが、私はこれを試していませんでした... *最大の顔面* ありがとうございました。それを指摘してくれてありがとうございます – mwaning

+0

うーん、うまくいきました!なぜ誰もが私の答えを落としてしまうのではないかと思ったら... – mrCarnivore

+0

特に、[リンクされた質問への回答](https://stackoverflow.com/a/23294659/3650362)が明らかに優れていて、 *あなたのコードのみの答えに欠陥がある理由を説明します* – trentcl

0
def defoption(): 
    print("Give an input") 
    option_input = input("Input: ") 
    return option_input 

opt = defoption() 
while(opt not in ['a']): 
    print("Option unavailable") 
    opt = defoption() 
print("Option {} works!".format(opt)) 
+0

私はあなたの答えをアップレターしました。なぜなら、私はトレントクの推論を分かち合っていないからです。 – mrCarnivore

+0

ありがとう、corn3lius。正確には私が探していたものではありませんが、まもなく間もなくループが役に立つと確信しています。 – mwaning

関連する問題