Python 练习实例31

python 练习实例31

python 编程100例python 编程100例

题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。。

程序源代码:

实例(python2.x):

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
letter = raw_input("please input:")
#while letter  != 'y':
if letter == 's':
    print ('please input second letter:')
    letter = raw_input("please input:")
    if letter == 'a':
        print ('saturday')
    elif letter  == 'u':
        print ('sunday')
    else:
        print ('data error')
    
elif letter == 'f':
    print ('friday')
    
elif letter == 'm':
    print ('monday')
    
elif letter == 't':
    print ('please input second letter')
    letter = raw_input("please input:")
 
    if letter  == 'u':
        print ('tuesday')
    elif letter  == 'h':
        print ('thursday')
    else:
        print ('data error')
        
elif letter == 'w':
    print ('wednesday')
else:
    print ('data error')

实例(python3.x):

#!/usr/bin/python3
 
letter = input("please input:")
#while letter  != 'y':
if letter == 's':
    print ('please input second letter:')
    letter = input("please input:")
    if letter == 'a':
        print ('saturday')
    elif letter  == 'u':
        print ('sunday')
    else:
        print ('data error')
    
elif letter == 'f':
    print ('friday')
    
elif letter == 'm':
    print ('monday')
    
elif letter == 't':
    print ('please input second letter')
    letter = input("please input:")
 
    if letter  == 'u':
        print ('tuesday')
    elif letter  == 'h':
        print ('thursday')
    else:
        print ('data error')
        
elif letter == 'w':
    print ('wednesday')
else:
    print ('data error')

以上实例输出结果为:

please input:s
please input second letter:
please input:a
saturday

python 编程100例python 编程100例

下一节:python 练习实例32

python 编程100例

相关文章