Python 練習實例31
python 練習實例31
題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續(xù)判斷第二個字母。
程序分析:用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或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')
以上實例輸出結(jié)果為:
please input:s please input second letter: please input:a saturday