2010-07-23 8 views
9

Pourquoi python me dit-il "TypeError: pow expected 2 arguments, a obtenu 3" malgré qu'il travaille dans IDLE (parfois il me dit que dans IDLE aussi bien)? Im simplement faire pow(a,b,c). mon programme est très court et je ne change pas la définition de pow à tout moment puisque je dois l'utiliser pour une certaine exponentiation.Pourquoi Python dit que pow n'a que 2 arguments

NOTE: Ceci est la pow de __builtin__, non Math

Répondre

14

Intégré pow prend deux ou trois arguments. Si vous faites from math import * alors il est remplacé par pow de math, qui prend seulement deux arguments. Ma recommandation est de faire import math, ou explicitement la liste des fonctions que vous utilisez dans la liste d'importation. Un problème similaire se produit avec open par rapport à os.open.

+0

ah ... c'est peut-être pourquoi. Merci!!!!! err ... une importation provenant d'un autre fichier l'affecterait-elle? J'important un autre programme que j'ai écrit qui a également 'de l'importation de maths' ' – calccrypto

+0

@calccrypto: Si vous importez l'autre programme avec' de p import * 'alors oui. Utilisez 'import p' ou listez explicitement' p import [...] '. – sdcvvc

0

http://docs.python.org/release/2.6.5/library/functions.html

pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z). The two-argument form pow(x, y) is equivalent to using the power operator: x**y.

The arguments must have numeric types. With mixed operand types, the coercion rules for binary arithmetic operators apply. For int and long int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, 102 returns 100, but 10-2 returns 0.01. (This last feature was added in Python 2.2. In Python 2.1 and before, if both arguments were of integer types and the second argument was negative, an exception was raised.) If the second argument is negative, the third argument must be omitted. If z is present, x and y must be of integer types, and y must be non-negative. (This restriction was added in Python 2.2. In Python 2.1 and before, floating 3-argument pow() returned platform-dependent results depending on floating-point rounding accidents.)

Peut-être vous violez la partie en gras?

+0

pas. je suis sûr que toutes les valeurs sont des entiers positifs éditer: yep. a, b, c = 9, 4, 225 – calccrypto

1

Si vous utilisez des fonctions mathématiques beaucoup et la version à trois paramètres de pow rarement un moyen de contourner cela en Python 2.7 est d'importer __builtin__ et appeler __builtin__ .pow pour les 3 paramete

+0

Il doit y avoir deux caractères de soulignement de chaque côté de 'builitin' dans les deux cas, mais la mise en forme l'interprète comme une police en gras - ne savez pas quoi faire à propos de thaat. –

Questions connexes