[075]python作業第六章-習題

6-1.可以string.find(str,beg=0,end=len(string))

6-2.稍後


6-3.

(a)>>> a=[1,2,5,6,7,3,4]

>>> sorted(a)

[1, 2, 3, 4, 5, 6, 7]

(b)


6-4

#!/usr/bin/env python

grades=raw_input('Type the grades:')

new_grades=grades.split()

another_new_grades=[]

for i in new_grades:

s=int(i)

another_new_grades.append(s)

for i in another_new_grades:

if i >=90:

print ('%s is A level') %i

elif i>=80:

print ('%s is B level') %i

elif i>=70:

print ('%s is C level') %i

elif i>=60:

print ('%s is D level') %i

else:

print ('%s is E level') %i

ave_grade=sum(another_new_grades)/len(another_new_grades)

print ('the average grade is %s' %ave_grade)


6-5

#!/usr/bin/env python

input_str=raw_input('Type a string:')

lens=len(input_str)

if lens==0:

print "You did not type string!"

if lens==1:

print input_str

for i,j in enumerate(input_str):

if i==0 and lens !=1:

print j,input_str[i+1]

elif i==lens-1 and i!=0:

print input_str[i-1],j

else:

print input_str[i-1],j,input_str[i+1]


6-6

#!/usr/bin/env python

import string

def my_str_strip(_str):

if len(_str) <=0:

return _str

while _str[0]==' ':

_str=_str[1:len(_str)]

while _str[len(_str)-1]==' ':

_str=_str[0:len(_str)-1]

return _str

print my_str_strip('     abc  abc   ')


6-7

#!/usr/bin/env python

# -*- coding: utf-8 -*-

num_str=raw_input('Enter the number:')

num_num=int(num_str)

fac_list=range(1,num_num+1)

print ("Before:",fac_list)

i=0

while i

print ("len(fac_list):,i",len(fac_list),i)

if num_num % fac_list[i]==0:

print (fac_list[i])

del fac_list[i]

else:

i=i+1

print "After:", fac_list


6-8

_min=input("Type tolmin:")

def min_to_hr(_min):

hr=0

_min2=0

_min/60=hr

_min%60=_min2

print hr,_min2

min_to_hr(_min)


6-9

minute=raw_input('Type the num:')

def hour_minute(minute):

minute = int(minute)

hour = minute // 60

minute = minute % 60

print(hour,':',minute)

hour_minute(minute)


6-10

input_str=raw_input("Type the string:")

new_m=[]

for i in input_str:

if i.isupper()==True:

new_m.append(i.lower())

else:

new_m.append(i.upper())

print new_m


6-12:

def findchr(string, char):

count=0

for i,j in enumerate(string):

print (i,j)

if char==j:

count=count+1

if count>0:

return True

else:

return False

print findchr('apple','e')

print findchr('banana','q')


6-13略

6-14

import random

user=raw_input("Type the number:")

user2=random.randint(0,3)

print ('user2:',user2)

if user==user2:

print 'same'

elif user>user2:

print 'win'

else:

print 'Lose'

你可能感兴趣的:([075]python作業第六章-習題)