2016-08-26 4 views
-4
Question1 = input("We will start off simple, what is your name?") 
if len(Question1) > 0 and Question1.isalpha(): #checks if the user input contains characters and is in the alphabet, not numbers. 
    Question2 = input("Ah! Lovely name, %s. Not surprised you get all the women, or is it men?" % Question1) 
    if Question2.lower() in m: #checks if the user input is in the variable m 
     print ("So, your name is %s and you enjoy the pleasure of %s! I bet you didnt see that coming." % (Question1, Question2)) 
    elif Question2.lower() in w: # checks if the user input is in the variable w 
     print ("So, your name is %s and you enjoy the pleasure of %s! I bet you didnt see that coming." % (Question1, Question2)) 
    else: #if neither of the statements are true (incorrect answer) 
     print ("Come on! You're helpless. I asked you a simple question with 2 very destinctive answers. Restart!") 
else: 
    print ("Come on, enter your accurate information before proceeding! Restart me!") #if the first question is entered wrong (not a name) 
Question3 = input("Now I know your name and what gender attracts you. One more question and I will know everything about you... Shall we continue?") 

複雑ではない、私はそれを実行したとき、私は最初に何が起こるかを教えてくれます: はいくつかのシナリオがありますが、私は2ときを説明します私は最初に私の名前が何であるか尋ねられたプログラムを実行する、私はアルファベットであるここに何かを入力することができる、それから私は男性や女性が好きかどうか私に尋ねる、私が知っている奇妙な私はそれが私が取り組んでいるプロジェクトです'男性'または '女性'のプログラムは完璧に動くが、もし私が言うと...「犬」と言うと、それはelseステートメントprint ("Come on! You're helpless. I asked you a simple question with 2 very destinctive answers. Restart!")に従うだろうが、コードを続けて上の最後の行に行く。あなたの名前とどのような性別があなたを魅了して......。 私がしようとしているのは、「男性」または「女性」以外のものを入力する場合はスクリプトを再起動することです。 私はしばらくの声明がうまくいくと言われましたが、なぜ、どのように説明する必要がありますか?私はある意味ではPythonを初めて使い慣れました。再起動のPythonプログラム内とif文 - 正しい答えを得るために

+0

正しい答えが与えられたらループを終了してループを終了します – Li357

答えて

0

これはループのためのものです。

while True: 
    # your input and all that here 
    if answer == "man" or answer == "woman": 
     # do what you want if the answer is correct 
     break # this is important, as it breaks the infinite `while True` loop 
    else: 
     # do whatever you want to do here :) 
     continue 
0

スクリプトが無限に繰り返したい場合は、単にそのようなwhileループですべてをラップすることができます:

while True: 
    # Do yo your logic here. 
    # Break out when a condition is met. 

あなたはそれでより多くの精通になりたい場合は、この方法を使用することができますし、それが完了するまで、メソッド自体を繰り返し呼び出すようにしてください。また

def ask_questions(): 
    # Do your logic here 
    if INPUT_NOT_VALID: 
     ask_questions() 
    else: 
     # Continue on. 

、あなたのテキストのためにこのサイト上の検索私はすべての私はと仮定しています、それはプログラミングクラスから共通の問題であることを有することを意味する。このプログラミングのシナリオ(1を含む約5または6の質問を見てきましたので、あなたがいる)。そのような場合は、クラスメートと話し合い、答えをオンラインで投稿するのではなく、お互いを刺激して解決策を見つけようとします。

+0

私は本当に様々な答えを感謝します、私はすぐにそれらの両方を調べます!残念ながら、私は楽しいためにプログラミングをしている高校生です。私は単独で学び、オンラインのクラスはありません。私はcodeacademy以外にも笑います。本当にありがとう、私は関連する質問についてサイトを必ず検索します。ありがとう –

関連する問題