判断奇偶
''' 创建一个函数(或在Shell中编写脚本),该函数接受一个整数作为参数,对于偶数返回“偶数”,对于奇数返回“奇数” 能被2整除的都是偶数 ''' #解法1 def even_or_odd(number): return 'even' if number%2==0 else 'odd' ''' 判断一串数字是不是奇偶排列, '''
#方法1
def oddAndEven(number): x=int(number[0])%2 for i in range(1,len(number)): if int(number[i])%2==x: return False else: x=int(number[i])%2 return True print(oddAndEven('01234567899'))
#方法2
def func(num):
... len_num=len(num)
... for i in range(1,len_num):
... if int(num[i])%2==int(num[i-1])%2:
... print("不是奇偶交替")
... return
... print("是奇偶交替")