2014-09-22 2 views
0

Я учусь Python 3 и теперь Iam пытается реализовать функцию факториала я писал:Python 3 факторный

fact = 1 
a = input('Enter number for factorial operation: ') 

for b in range (1, a+1, 1): 
    fact = fact * b 

print ("Sum is ", fact) 

он говорит:

for b in range (1, a+1, 1): 
TypeError: Can't convert 'int' object to str implicitly 
+0

Может быть интересны: http://stackoverflow.com/a/25111524/ 198633 – inspectorG4dget

ответ

3

Это происходит потому, что input возвращает вам str, не int, это то, что вам нужно. Вы можете исправить это путем литья str в int следующим образом:

a = int(input('Enter number for factorial operation: ')) 

Взгляните на это:

In [68]: a = input('Enter number for factorial operation: ') 
Enter number for factorial operation: 5 

In [69]: a 
Out[69]: '5' 

In [70]: type(a) 
Out[70]: str 

In [71]: isinstance(a, str) 
Out[71]: True 

In [72]: isinstance(a, int) 
Out[72]: False 

In [73]: a = int(input('Enter number for factorial operation: ')) 
Enter number for factorial operation: 5 

In [74]: a 
Out[74]: 5 

In [75]: type(a) 
Out[75]: int 

In [76]: isinstance(a, str) 
Out[76]: False 

In [77]: isinstance(a, int) 
Out[77]: True 
Смежные вопросы