2010-07-23 11 views
9

Perché Python mi sta dicendo "TypeError: pow si aspettavano 2 argomenti, ottenuto 3" nonostante funzionasse in IDLE (a volte mi dice che anche in IDLE)? Sto semplicemente facendo pow(a,b,c). il mio programma è molto breve e non cambio la definizione di pow in qualsiasi momento poiché ho bisogno di usarlo per un po 'di esponenziazione.Perché Python dice che pow ha solo 2 argomenti

NOTA: Questa è la pow da __builtin__, non Math

risposta

14

Built-in pow accetta due o tre argomenti. Se si fa from math import * allora viene sostituito da pow di matematica, che richiede solo due argomenti. La mia raccomandazione è di fare import math, o elencare esplicitamente le funzioni che usi nella lista di importazione. Il problema simile si verifica con open rispetto a os.open.

+0

ah ... forse è per questo. Grazie!!!!! err ... potrebbe importare un file da un altro file? Im importando un altro programma ho scritto che ha anche da import math * – calccrypto

+0

@calccrypto: Se stai importando un altro programma con 'da p import *' allora sì. Usa 'importa p' o elenca esplicitamente' da 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.)

forse stai violando la parte in grassetto?

+0

no. sono sicuro che tutti i valori sono interi positivi modifica: sì. a, b, c = 9, 4, 225 – calccrypto

1

Se si utilizza funzioni matematiche molto e la versione dei parametri e tre pow rado un modo per aggirare questo in Python 2.7 è importare __builtin__ e chiamare __builtin__ .pow per il 3 paramete

+0

Ci dovrebbero essere due caratteri di sottolineatura su entrambi i lati di "builitin" in entrambi i casi, ma la formattazione lo ha interpretato come carattere in grassetto - non sono sicuro di cosa fare riguardo a thaat. –

Problemi correlati