python习题一

1.26个字母大小写成对打印,例如:Aa,Bb......

方法1:

for i in range(26):
  print(chr(65+i)+chr(97+i))

方法2:

for i in range(26):
  print(chr(ord("A")+i),chr(ord("a")+i))

2.一个list包含10个数字,然后生成一个新的list,要求新的list里面的数都比之前的数多1

s = list(range(10))
list = []
for i in s:
  list.append(i+1)
print(list)

3.倒序取出每个单词的第一个字母,例如:I am a good boy!

方法1:

s = "I am a good boy!"
s = s.split()
s.reverse()
result = []
for i in s:
  result.append(i[0])
print(result)

方法2:

a = "I AM A BOY"
result = []
for i in a.split()[::-1]:
  result.append(i[0])
print(result)

4.输入一个自己的生日月份,用if和else判断一下当月是不是你的生日月

import time
month = input("send a month: ")
current_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
#print(type(current_time.split("-")[1]))
#print(current_time.split("-")[1])
if current_time.split("-")[1] == month:
  print("This month is mybirthday!")
else:
  print("This month is not mybirthday!")

5.输入3个字母:e、a、r,如果输入e,那么退出循环,如果输入a,执行continue,如果输入r,那么再读取一次字母,并打印,用死循环实现。

while 1:
letter = input("send a letter:")
if letter == 'r':
letter = input("send a letter:")
elif letter == 'e':
break
elif letter == 'a':
continue

6.输入3个字母:e、a、r,如果输入e,那么退出循环,如果输入a,执行continue,如果输入r,那么再读取一次字母,并打印,只允许输入三次字母,重复输入的字母不算在内。

count = 0
for i in range(3):
letter = input("send a letter%d:"%i)
if letter == 'e':
break
elif letter == 'a':
continue
elif letter == 'r':
count += 1
if count == 2:
input("send a letter dddd:")
P.S感觉这道题目可能有问题,哈哈。。。

7.把一个字符串"abcdefg"插入到一个list中,每个字母占一个list中的元素位置,例如:["a","b",
"c","d","e","f","g"]

s = "abcdefg"
lists = []
for i in s:
  lists.append(i)
  print(lists)
#反思
a = ["a","b","c","d","e","f","g"]
print("".join(a))

8.['a','b','c','d','e','f','g']操作这个list,拼出一个字符串"adg"

s = ['a','b','c','d','e','f','g']
print("".join(s[::3]))

9.做一个小图书馆程序,图书馆的书放到一个list里保存,使用add命令:添加一本书,使用lend命令:减去一本书,如果不存在,要提示一下,使用getall命令:可以查询所有没有借出去的书,然后退出程序。

library = []
while 1:
  command = input("your command and book name is: ")
  command = command.strip()
  if "add" in command:
    library.append(command.split(" ")[1])
    print("add book complete!")
  elif "lend" in command:
    if command.split(" ")[1] in library:
      library.remove(command.split(" ")[1])
      print("lend book complete!")
    else:
      print("The book your lend doesn't exist!")
      continue
  elif "getall" in command:
    if len(library) >= 1:
      for book in library:
        print(book)
    else:
      print("No book in the library!")
  elif "." == command:
    print("Wrong!")
  else:
    print("Bye!")
    break

10.写一个函数,统计一下一句话中的数字个数。例如:I am a 19 years old boy!666!

a = "I am a 19 years old boy!666!"
def total_num(s):
  if not isinstance(s,str):
    print("is not str!")
  return 0
  result = 0
  for i in s:
    if i in "1234567890":
      result += 1
  return result

print(total_num(a))

P.S: 数字也可以用string.digits表示,但是要import string

11.写一个函数,使用可变参数*arg,计算函数所有参数之和。

def sum(*arg):
  sum = 0
  for i in arg:
    sum += i
  return sum

print(sum(1,5,7,8,8))

12.写一个函数,使用可变参数**kw,输出的key、value。

def key_value(**kw):
  for k,v in kw.items():
  print(k,":",v)

print(key_value(a=2,b=3,c=5))

13.使用**kw,把可变的所有参数算一个乘积。

def key_value(**kw):
  sum = 1
  for v in kw.values():
    sum *= v
  print(sum)

key_value(a=4,b=5,c=5)

14.同时使用*arg和**kw,算一下字母的长度之和,注意所有参与数均使用字符串,字符串都是字母。

def total(*arg,**kw):
    sum = ""
    for i in arg:
        sum += i
    for i in kw.values():
        sum += i
    print(len(sum))


total("a","b","c",b="a",d="c")

15.统计一下一共出现了多少个单词,每个英文单词出现的次数。例如:I am a boy.I am a good man.

s = "I am a boy.I am a good man."
s = s.replace("."," ")
s = s.split(" ")
s = s[:-1]
num = {}
for i in s:
  count = s.count(i)
  num[i] = count


print(num)

16.判断1-127阿斯克码里都是什么字符。

while 1:
  number = input("send a num: ")
  number = int(number)
  if number >=1 and num <= 127:
    ascii = chr(number)
    print(ascii)
  else:
    print("Not in 1-127!")
    break

17.输入a、b、c、d,四个整数,计算a+b-c*d的结果。

while 1:
  a = input("send a is: ")
  b = input("send b is: ")
  c = input("send c is: ")
  d = input("send d is: ")
  if a and b and c and d:
    int_a = int(a)
    int_b = int(b)
    int_c = int(c)
    int_d = int(d)
    result = int_a+int_b-int_c*int_d
    print(result)
  else:
    print("Bye!")
    break

18.3个人在餐厅吃饭,想分摊饭费。总共花费35.27美元,他们还想给15%的小费,每个人该怎么付钱。

per_fee = round((35.27+35.27*0.15)/3.2)

19.初始化一个车里有多少升油,调用run方法,实现车的运行,没有的时候提示:需要加油。跑一次里程数需要加100公里,汽油减少20.调用add_gas方法可以实现加油,让车继续运行,check_distance方法查看车跑的里程数。

class car():
    def __init__(self,gas_volume,gas_valume_num,distance):
        self.gas_volume = gas_volume
        self.gas_valume_num = gas_valume_num
        self.distance = distance

    def run(self):
        if self.gas_volume > = self.gas_valume_num:
            self.distance += 100
            self.gas_volume -= 20
       else:
            print("need to add gas!")


    def add_gas(self,n):
        self.gas_volume += n

    def check_distance(self):
        return self.distance

    def check_gas(self):
        return self.gas_volume

c = car(100,20,0)
for i in range(3):
    c.run()
    print("check gas is :",c.check_gas())
print("check distance is :",c.check_distance())
c.add_gas(100)
print("check gas is :",c.check_gas())

20.统计一下,当前目录下的文件数量总和。

import os
file_count = 0
dir_count = 0
for i in os.listdir("e:\\"):
    if os.path.isdir(i):
        dir_count += 1
    else:
        file_count += 1

 

print(file_count)

print(dir_count)

21.程序建立一个10级的目录pic1到pic10。

import os
os.makedirs("e:\\pic")
os.chdir("e:\\pic")
for i in range(1,11):
    os.mkdir("pic"+str(i))
    os.chdir("pic"+str(i))

22.写一个函数,统计一下字母个数。例如:I am a 19 years old boy!666!

s = "I am a 19 years old boy!666!"

def letter_nums(s):
  list = []
  for i in s:

    if i >= chr(65) an

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值