2010-07-23 20 views
9

Dlaczego Python mówi mi "TypeError: pow oczekiwano 2 argumenty, ale 3" pomimo pracy w IDLE (czasami mówi mi to również w IDLE)? po prostu robię pow(a,b,c). mój program jest bardzo krótki i nie zmieniam definicji pow w dowolnym momencie, ponieważ potrzebuję go użyć dla jakiejś potęgi.Dlaczego Python mówi pow tylko ma 2 argumenty

UWAGA: Jest to pow z __builtin__ nie Math

Odpowiedz

14

Wbudowany pow ma dwa lub trzy argumenty. Jeśli wykonasz from math import *, to zostanie zastąpiony przez matematykę pow, która przyjmuje tylko dwa argumenty. Moją rekomendacją jest wykonanie import math lub jawne wylistowanie funkcji używanych na liście importu. Podobny problem występuje z open w stosunku do os.open.

+0

ah ... może to dlatego. dzięki!!!!! err ... czy import z innego pliku wpłynąłby na to? importuję inny napisany przeze mnie program, który również ma 'z matematyki import *' – calccrypto

+0

@calccrypto: Jeśli importujesz inny program z 'from p import *', to tak. Użyj 'import p' lub list jawnie' from 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.)

Może jesteś naruszono pogrubiony fragment?

+0

nie. Jestem pewien, że wszystkie wartości są liczbami całkowitymi dodatnimi edytuj: tak. a, b, c = 9, 4, 225 – calccrypto

1

Jeśli używasz funkcji matematycznych dużo i wersję trzy parametru pow rzadko sposób obejść ten problem w Pythonie 2.7 jest import __builtin__ i nazywają __builtin__ .pow do 3 Parametrów

+0

Powinny występować dwa podkreślenia znaków po obu stronach "builitin" w obu przypadkach, ale format interpretowany jako pogrubiona czcionka - nie wiem, co zrobić z tym. –

Powiązane problemy