python基础2练习

来源:互联网 发布:网络机房线很乱 编辑:程序博客网 时间:2024/05/17 12:24
1.要求输入一个英文句子,统计该英文句子中含有的单词数量及词的种类
 例:
 输入: i am very very happy
 输出: 5 4
 
 主要代码:
 a = raw_input('输入一个英文句子:')
 str = a.split()
 str1 = set(str)
 print len(str),len(str1)
 
2.要求实现栈的部分功能如下:
 o|O(pop):出栈
 u|U(push):入栈
 v|V(view):查看栈的元素
 q|Q(quit):退出
 主要代码:
 stack=[]
 def menu():
     print '''
      ******欢迎来到栈处理系统******
      请输入以下操作
  o|O(pop)            u|U(push)
  v|V(view)           q|Q(quit)'''
 menu()
 def pushstack():
     add = raw_input('请输入要入栈的元素:')
     stack.append(add)
     print '增加元素后的栈为:',stack
 def popstack():
     if len(stack)<1:
  print "无数据可弹出"
     else :
  stack.pop(-1)
  print '删除元素后的栈为:',stack
 def viewstack():
     print stack
 while True:
     choice = raw_input('请输入您的操作:').lower()
     if choice=="v":
  viewstack()
     elif choice=="o":
  popstack()
     elif choice=="u":
  pushstack()
     elif choice=="q":
  print '''
       *********退  出**********
       '''
  exit(0)
     else:
  print 'input v|o|u|q'
 










原创粉丝点击