2016-04-16 17 views
0

私は「DEF」のコマンド値は機能しませんか?

 print("Hello World") 
nb=input(("Insérer le nombre à multiplier : ")) 
max=input(("Combien de fois voulez vous multiplier ?: ")) 
print("Bien, maintenant utilisez la commande table") 
def table(nb, max): 
    i = 0 
    while i < max: 
     print(i + 1, "*", nb, "=", (i + 1) * nb) 
     i += 1 # On incrémente i de 1 à chaque tour de boucle. 
    else: 
     print("Calcul Terminé ,-D") 

を使用して簡単なコマンドを作りたいので、例えばコマンドテーブル(8,9)は完璧に動作しますが、テーブルには動作しませんが、私は理解していないことということです

 table() 
TypeError: table() missing 2 required positional arguments: 'nb' and 'max' 

これらの引数が定義されたものは、デフォルトでは、次のとおりです。コマンドテーブルには値がコマンド自体に定義されていない場合でも動作するはずですので、前に定義されているNB値と最大値は、代わりにそれは私にエラーを表示します最初の視点でユーザによって、ここでは変数nbのように動作し、maxはまだ値がありません。これは、前に定義したので間違っています私の方法の仕組みをどうやって作るのか、それとも私のロジックの中でプログラムが意図するように動作する最短のコードは何ですか?

ありがとうございました!

答えて

0

グローバル変数としてnbmaxを定義する必要があります。これらは、table()のメソッド内にも表示されます。私はまた、算術演算として、あなたのコードでintに型キャストを追加

global nb 
nb = int(input(("Insérer le nombre à multiplier : "))) 
global max 
max = int(input(("Combien de fois voulez vous multiplier ?: "))) 
print("Bien, maintenant utilisez la commande table") 
def table(): 
    i = 0 
    while i < max: 
     print(i + 1, "*", nb, "=", (i + 1) * nb) 
     i += 1 # On incrémente i de 1 à chaque tour de boucle. 
    else: 
     print("Calcul Terminé ,-D") 

table() 

注意は文字列では許可されていない、とinput()は、文字列型の値を返します。

関連する問題