第六章

第2题
#!/usr/bin/env python
import string
import keyword
firstLetter = string.letters + '_'
letterOnLevel = firstLetter + string.digits
print "Please Input Words To Test:",
testWord = raw_input()
if len(testWord) == 1:
if testWord in firstLetter:
print "%s Is On Level."%testWord
else:
print "%s Isn't On Level."%testWord
exit()

if testWord[0] not in firstLetter:
print "%s Isn't On Level."%testWord
exit()

for ch in testWord[1:]:
if ch not in letterOnLevel:
print "%s Isn't On Level."%testWord
exit()

if testWord in keyword.kwlist:
print "%s Is An Keyword,And It's Not On Level."%testWord
else:
print "%s Is On Level."%testWord


第3题

#!/usr/bin/env python
import string
ar = []
Count = 0
switches = True
while switches:
print "Please Input %dth Digit:"%(Count+1),
chs = raw_input()
for ch in chs: #test the inputs is all digit or not
if ch not in string.digits:
switches = False
break
if switches == False:
continue
ar.append(int(chs))
Count += 1

print ar
ar.sort()
print ar
ar[-1::-1] = ar
print ar

第4题

#!/usr/bin/env python
examList = []
Count = 0
while Count != 5:
print "Please Input %dth Result Of Exam:"%(Count+1),
chs = raw_input()
examList.append(float(chs))
Count += 1

avgExam = float(sum(examList)) / 5

print"Result Is %.2f "%avgExam
if avgExam <= 100 and avgExam >= 90:
print "A"
elif avgExam < 90 and avgExam >= 80:
print "B"
elif avgExam < 80 and avgExam >= 70:
print "C"
elif avgExam < 70 and avgExam >= 60:
print "D"
elif avgExam < 60:
print "E"

第5题

#!/usr/bin/env python
print"Please Input Any Word To Test:",
testWord = raw_input()
for ch in testWord:
print"%s"%ch,
print
for ch in testWord[-1::-1]:
print"%s"%ch,


#!/usr/bin/env python
print "Please Input First Word:",
word_1 = raw_input()
print "Please Input Second Word:",
word_2 = raw_input()
if len(word_1) != len(word_2):
print"!="
exit()

Count = 0
for ch in word_1:
if ch != word_2[Count]:
print"!="
exit()
Count += 1

print "=="


#!/usr/bin/env python
print "Please Input Any Word To Test:"
testWord = raw_input()
Count = len(testWord) - 1
for ch in testWord:
if ch == testWord[Count]:
Count -= 1
else:
break

if Count == -1:
print "=="
else:
print "!="



#!/usr/bin/env python
print "PLease Input Any Word To Test:",
testWord = raw_input()
rTestWord = ""
for ch in testWord[-1::-1]:
rTestWord += ch
testWord += rTestWord
print testWord


第6题
#!/usr/bin/env python
def myStrip(strs):
newString = ""
Lens = len(strs)
if strs[0] == " " and strs[-1] == " ":
for ch in strs[1:Lens-1]:
newString += ch
return newString

if strs[0] == " ":
for ch in strs[1::]:
newString += ch
return newString

if strs[-1] == " ":
for ch in strs[:Lens-1:]:
newString += ch
return newString

return strs

print "PLease Input Any Word To Test:",
testWord = raw_input()
print testWord
print "~~~~~~%s~~~~~"%(myStrip(testWord))


第7题

#!/usr/bin/env python
num_str = raw_input("Enter a number:")
num_num = int(num_str)
fac_list = range(1,num_num+1)
print fac_list
i = 0
Count = 0
allNu = len(fac_list)
while i < allNu:
if num_num%fac_list[Count] == 0:
del fac_list[Count]
else:
Count += 1
i += 1
print fac_list

第8题

#!/usr/bin/env python
print "Please Input Any Intager To Test(0-1000):",
testInt = raw_input()
for x in testInt:
x = int(x)
if x == 0:
print "Zero",
if x == 1:
print "One",
if x == 2:
print "Two",
if x == 3:
print "Three",
if x == 4:
print "Four",
if x == 5:
print "Five",
if x == 6:
print "Six",
if x == 7:
print "Seven",
if x == 8:
print "Eight",
if x == 9:
print "Nine",


第9题

#!/usr/bin/env python
def changeTime(M):
ms = M % 60
hs = M / 60
return [hs,ms]

Ms = raw_input("Please Input Time(Min):")
print changeTime(int(Ms))

第10题

#!/usr/bin/env python
testWord = raw_input("Please Input Any Word To Test:")
newWord = ""
for ch in testWord:
if not ch.isalpha():
newWord += ch
elif ch.isupper():
newWord += ch.lower()
elif ch.islower():
newWord += ch.upper()
print newWord

第12题

#!/usr/bin/env python
def findChar(tarString,ch):
Len = 1
for c in tarString:
if c == ch:
break;
Len += 1
if Len == len(tarString):
return - 1
else:
return Len - 1

def rFindChar(tarString,ch):
Len = len(tarString) - 1
for c in tarString[-1::-1]:
if c == ch:
break;
Len -= 1
if Len == 0:
return - 1
else:
return Len

def subChar(tarString,ch,newChar):
newString = ""
for c in tarString:
if c == ch:
newString += newChar
else:
newString += c
return newString

print "Please Input Any Word To Test:",
testWord = raw_input()
print "Please Input The Char That's You Want To Find:",
chs = raw_input()
print findChar(testWord,chs)
print rFindChar(testWord,chs)
print "Please Input An New Char To Turn:",
newCh = raw_input()
print subChar(testWord,chs,newCh)

第14题

#!/usr/bin/env python
import random
def Guess(result):
computerResult = random.randint(-1,1)
if result == computerResult:
print "Even"
elif computerResult + result != 0:
if computerResult > result:
print"You Lose"
else:
print"You Win."
elif computerResult + result == 0:
if computerResult < result:
print"You Lose"
else:
print"You Win."

print "Please Input You Choose."
print "A:'R'ock B:'P'aper C:'S'cissors"
result = raw_input()
if result == 'R':
Guess(-1)
elif result == 'P':
Guess(0)
elif result == 'S':
Guess(1)

第15题

#!/usr/bin/env python
import math
def countDays(list_1,list_2): #list MM/DD/YY
Day_1 = int(list_1[3:5])
Month_1 = int(list_1[0:2])
Year_1 = int(list_1[6:8])

Day_2 = int(list_2[3:5])
Month_2 = int(list_2[0:2])
Year_2 = int(list_2[6:8])

AllDay_1 = Year_1 * 360 + Month_1 * 30 + Day_1 #I 1Year = 12Months = 360Days
AllDay_2 = Year_2 * 360 + Month_2 * 30 + Day_2

return abs(AllDay_1 - AllDay_2)

print countDays("02/20/98","04/29/97")

def brithday(listDay): # list MM/DD/YYYY
days = int(listDay[3:5])
months = int(listDay[0:2])
years = int(listDay[6:10])
allDay = years * 365 + months * 30 + days
today = 2016 * 365 + 9 * 30 + 15 #Today is 2016-9-15
print"You Had Lived In This World For %d Days."%(today-allDay)
brithday("05/24/1995")


第16题

#!/usr/bin/env python
def sumMaritx(list_1 , list_2):
X = int(list_1[0]) + int(list_2[0])
Y = int(list_1[1]) + int(list_2[1])
return [X,Y]

def mulMaritx(list_1 , list_2):
X = int(list_1[0]) * int(list_2[0])
Y = int(list_1[1]) * int(list_2[1])
return [X,Y]

print sumMaritx([3,1],[4,2])
print mulMaritx([3,1],[4,2])

第17题

#!/usr/bin/env python
def myPop(tarList):
Len = len(tarList)
del tarList[Len-1]
return tarList

print "PLease Input Any Word To Test:",
testWord = raw_input()
print myPop(list(testWord))

第19题

#!/usr/bin/env python
import random
def printColumn(tarList,column):
rows = len(tarList) / column
for i in range(0,column):
beginIndex = rows * (i)
if i == column - 1: # Last Column
endIndex = len(tarList)
else:
endIndex = rows * (i + 1)
print tarList[beginIndex:endIndex]

listInt = []
#for i in range(0,99):
# listInt.append(random.randint(0,99))
i = 1
while i <= 100:
listInt.append(i)
i += 1
print "Please Input Column:"
col = raw_input()
printColumn(listInt,int(col))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值