Python ('NoneType')

Модератор: Модераторы разделов

Аватара пользователя
GFORGX
Сообщения: 47
ОС: ArchLinux

Python

Сообщение GFORGX »

Имеется код:

Код: Выделить всё

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def year_count():
        year=raw_input('Year: ')
        if len(year)==4:
                return int(year[0])+int(year[1])+int(year[2])+int(year[3])
        else:
                year_count()
print type(year_count())


Если сразу ввести 4-значное, то всё ОК:

Код: Выделить всё

[gforgx@arch ~]$ ./script.py
Year: 1992
<type 'int'>

Если ввожу не 4-значное, а потом 4-значное, то:

Код: Выделить всё

[gforgx@arch ~]$ ./script.py
Year: 1111111
Year: 1992
<type 'NoneType'>


Почему такое происходит?
Give and get back some - sharing it all,
Path we know best - we're having a ball
Give and get zeros, give and get ones
Given to you but not you to us.
Opulent mission lost in our passion -
You can still choose if you don't swim to win - you'll never lose!
Спасибо сказали:
Аватара пользователя
Skladnoy
Сообщения: 90
ОС: Debian

Re: Python

Сообщение Skladnoy »

Потому что:

Код: Выделить всё

def year_count():
    year=raw_input('Year: ')
    if len(year)==4:
        # Возвращает год (а почему не int(year) ?)
        return int(year[0])+int(year[1])+int(year[2])+int(year[3])
    else:
        year_count()
   # Возвращает None
Спасибо сказали: