2017-06-04 9 views
2

私は簡単な調査ボットを作ろうとしていましたが、2番目のif文に問題がありました。 ifとelifを無視し、elseステートメントにまっすぐ進みます。なぜ私のプログラムはブロックされたらブロックするのですか?

あなたは豆たちから連絡しているどのように多くの時間:私が手出力だけである

...助けてください
import sys 

yes = "yes" 
no = "no" 
experience_good = "yes" 
contact = "1" 

print("How many times have you bean contacted by us in the past quarter (3 months)") 
contact = sys.stdin.readline() 

if contact != 0: 
    print("Was your experience was us good? Type a 1 for yes or 0 for no") 
    experience_good = sys.stdin.readline() 
    print("experiencence_good is", experience_good) 
    # The above line was just to double check the variable was inputted 
correctly 
    if experience_good is 1: 
     print("Good to hear that.") 
    elif experience_good is 0: 
     print("Sorry about that. What could we be doing better?") 
     doing_better = sys.stdin.readline() 
    else: 
     print("Stuff's been messed up") 

を私はすべてを試してみました、そしてそれはおそらく簡単な解決策になるだろうにもかかわらず、過去の四半期(3ヶ月 )に

は、あなたの経験は、私たちは良いしてましたか? 1はい、または0を入力していない

ためexp_goodはexperience_good1ことはありませんので、1人の

スタッフが

+0

これはどの言語ですか? –

+0

@Sergio Tulentsevと@Emerson SD、皆さんは少しミスを犯しました。 'contact = sys.stdin.readline()'は整数ではない文字列を返し、 'contact!= 0'をチェックしています。修正してください。正しく動作するはずです – theBuzzyCoder

答えて

1

をめちゃめちゃにされていますです!それは保持する変数ではない文字列値'1'が何であるかをユーザーが1で入力した場合は多分は、この時点で

experience_good = sys.stdin.readline() 

ように変更途中でそれを次に

experience_good = "yes" 

、とオフを開始しています1ので、あなたはあなたがを使用する必要があります

experience_good = int(sys.stdin.readline().strip()) 
+0

おかげさまでヒープ! –

1

に必要int型の等価性テストのための:

if contact != 0: 
    print("Was your experience was us good? Type a 1 for yes or 0 for no") 
    experience_good = sys.stdin.readline() 
    print("experiencence_good is", experience_good) 
    # The above line was just to double check the variable was inputted correctly 
    if experience_good == 1: 
     print("Good to hear that.") 
    elif experience_good == 0: 
     print("Sorry about that. What could we be doing better?") 
     doing_better = sys.stdin.readline() 
    else: 
     print("Stuff's been messed up") 

キャストcontactexperience_goodintへ:(今の質問に編集した)

contact = int(contact) 

experience_good = int(experience_good) 
1

インデントの問題。最後のelifと最後のelseを1つだけインデントします。

if experience_good == 1: 
    print("Good to hear that.") 
elif experience_good == 0: 
    print("Sorry about that. What could we be doing better?") 
    doing_better = sys.stdin.readline() 
else: 
    print("Stuff's been messed up") 

==を使用して値を評価します。 'is'を使用すると、オブジェクトを比較します。それは価値ではありません。

+0

なぜそれがインデントだと思いますか? –

+0

元のリビジョンではインデントが問題なく、問題はありませんでした。 (その後の編集でうんざりしてしまった) –

+0

インデントは固定されています。あなたはまた、出力の最後の行を修正することができます:ものは –

関連する問題