2017-11-16 2 views
1

私は現在、ユーザがどのような色のカーペットを入力し、どのカーペットとどの領域を入力したかによって異なる価格を与えるプログラムを作成しようとしています。問題は、私は非常にPythonとプログラミングの両方を使用して新しいですので、パラメータを正しく使用しています。現在のプログラム仕様では、サブルーチンを使用する必要があります。問題の例は、私の最後の行main(exit1)で、exit1が定義されていないと言い、main()にコードを編集しようとすると、exit1が必要だということです。どんな助けでも大歓迎です。複数のサブルーチンでパラメータを使用するpython

あなたがスコープ内に定義する(あなたはあなたのプログラムの最後の行に main関数への引数として変数を渡ししようとしているが、その行が実行される前に、あなたが exit1変数を定義していない
def prices(): 
    PriceA = 40 
    PriceB = 50 
    PriceC = 60 
def main(exit1): 
    exit1 = False 
    while exit1 == False: 
     carpet = str(input("What carpet would you like blue, red or yellow ")) 
     carpet = carpet.upper() 
     if carpet == "X": 
      exit1 = True 
     else: 
      width = int(input("What is the width of the room in M^2 ")) 
      height = int(input("What is the height of the room in M^2 ")) 
      area = width * height 


      if carpet == "BLUE":   
       a(area) 

      elif carpet == "RED": 
       b(area) 

      elif carpet == "YELLOW": 
       c(area) 


      else: 
       print("Invalid carpet") 
       cost = 0 
       output(cost) 





def output(cost, exit1): 
print ("the price is £", cost) 


def a(area, PriceA): 
    cost = PriceA * area 
    output(cost) 

def b(area, PriceB): 
    cost = PriceB * area 
    output(cost) 

def c(area, PriceC): 
    cost = PriceC * area 
    output(cost) 

main(exit1) 

答えて

0

main関数の)。あなたがしたいことを達成するためには、mainexit1引数を指定する必要はないので、関数定義と関数呼び出しからそれを削除することができます。

def main(): 
    ... 

... 

main() 
0

問題は、変数を関数に入力する前に定義する必要があるという問題です。私はコードを修正し、いくつかの修正を加えました。また、インデントエラーがあり、呼び出したときに関数に十分な引数を与えられていないように見えました。書かれたコードはちょっとしたことがあります。私が何かしたことが分かっていないのであれば、メッセージを送ってください。そうでなければ、code acadamyやpythonprogramming .netのようなサイトを勧めます。 def main():

PriceA = 40 
    PriceB = 50 
    PriceC = 60 
    while True: 
     carpet = str(input('What carpet would you like blue, red or yellow? ')) 
     carpet = carpet.upper() 
     if carpet == 'X': 
      break 
     else: 
      width = int(input("What is the width of the room in M? ")) 
      height = int(input("What is the height of the room in M? ")) 
      area = width * height 

     if carpet == "BLUE":   
      output(area,PriceA) 

     elif carpet == "RED":   
      output(area,PriceB) 

     elif carpet == "YELLOW":   
      output(area,PriceC) 
     else: 
      print('Invalid carpet') 
def output(area,Price): 
    cost = area*Price 
    print('the price is £',cost) 
main() 
関連する問題