2009-05-16 4 views
-1

私は非常に新しいので、ちょうど学ぶので、簡単にしてください!このPythonコードで何が問題になっていますか?

start = int(input('How much did you start with?:')) 
if start < 0: 
    print("That's impossible! Try again.") 
    print(start = int(input('How much did you start with:'))) 
if start >= 0: 
    print(inorout = raw_input('Cool! Now have you put money in or taken it out?: ')) 
    if inorout == in: 
     print(in = int(raw_input('Well done! How much did you put in?:'))) 
     print(int(start + in)) 

これは常に構文エラーになりますか?私は間違った何かをしていると確信しています!

ありがとうございます!

+0

上で実行しないように、メイン機能でスクリプトの実行をラップraw_input代わり

  • を使用し、一般的なユーザ入力をinputを使用しないでくださいあなたはこれを実行していますPython 3または2.xを介して? –

  • 答えて

    7
    • あなたは、Cのように、Pythonで表現内の変数に代入することはできません。プリントは、(= INT(入力(「何とか」))開始)正しくありません。個別のステートメントで最初に割り当てを行います。
    • 最初の行はインデントされていませんが、コピーと貼り付けのエラーの可能性があります。あなたの問題であり、あなたは書類に割り当てた変数名
    +0

    そして 'in'はif-conditionで定義されておらず、入力(python 3)かraw_inputのいずれかがあります。 – Dario

    +0

    はinorout == "in"であったはずですが、予約語は変数名として意図されていません。 – gimel

    3

    のためにそれを使用することはできませんので

  • 言葉inは予約語です。 割り当てを中止するステートメントを移動する

  • +0

    printはここの関数です。 – SilentGhost

    0
    • ループをラップする関数を使用して入力を促すことを検討してください。
    • それは輸入

    def ask_positive_integer(prompt, warning="Enter a positive integer, please!"): 
        while True: 
         response = raw_input(prompt) 
         try: 
          response = int(response) 
          if response < 0: 
           print(warning) 
          else: 
           return response 
         except ValueError: 
          print(warning) 
    
    def ask_in_or_out(prompt, warning="In or out, please!"): 
        ''' 
        returns True if 'in' False if 'out' 
        ''' 
        while True: 
         response = raw_input(prompt) 
         if response.lower() in ('i', 'in'): return True 
         if response.lower() in ('o', 'ou', 'out'): return False 
         print warning 
    
    def main(): 
        start = ask_positive_integer('How much did you start with?: ') 
        in_ = ask_in_or_out('Cool! Now have you put money in or taken it out?: ') 
        if in_: 
         in_amount = ask_positive_integer('Well done! How much did you put in?: ') 
         print(start + in_amount) 
        else: 
         out_amount = ask_positive_integer('Well done! How much did you take out?: ') 
         print(start - out_amount) 
    
    if __name__ == '__main__': 
        main() 
    
    関連する問題