设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 25399|回复: 2
打印 上一主题 下一主题

【Python 第30课】 字符串的索引和切片

[复制链接]

0

主题

2

好友

72

积分

注册会员

Rank: 2

楼主
发表于 2018-4-8 21:22:20 |显示全部楼层
本节练习代码:
  1. word='hello world!'
  2. for i in word:
  3.     print(i)

  4. print(word[0])
  5. print(word[1:4])

  6. new_word=','.join(word)
  7. print(new_word)
  8. new_word2='.'.join(word)
  9. print(new_word2)
  10. new_word3=''.join(word)
  11. print(new_word3)

  12. new_word1=word.split()
  13. print(new_word1)
  14. x=['a','b','c']
  15. x1=','.join(x)
  16. print(x1)

  17. x2=['ab','cd','ex']
  18. x3=''.join(x2)
  19. print(x3)
复制代码
回复

使用道具 举报

0

主题

2

好友

72

积分

注册会员

Rank: 2

沙发
发表于 2018-4-8 22:24:16 |显示全部楼层
本帖最后由 司南 于 2018-4-8 22:32 编辑

终于整出来了,总结经验:提前画好流程,整理思路,事半功倍
有个问题想请问一下Crossin先生,提前结束的部分,感觉自己滥用了break,请问有其他的解决办法啥的麽?
  1. #football_game.py
  2. #司南 2018.04.07
  3. #方向设定
  4. direction=['left','middle','right']
  5. #扑救方
  6. def keeper(name='computer'):
  7.     import random
  8.     dic=random.choice(direction)
  9.     if name=='computer':
  10.         print('天哪,快看,守门员扑向了%s方向' %dic)
  11.     else:
  12.         print('天哪,快看,守门员%s扑向了%s方向' %(name,dic))
  13.     return dic
  14. #射门方
  15. def shoot(man):
  16.     dic=input('请输入射球方向:left/middle/right')
  17.     print('快看,%s能否射进呢?' %man)
  18.     return dic
  19. #比赛规则
  20. def fight(keeperdic,shootdic):
  21.     if keeperdic==shootdic:
  22.         print('omg,the goalkeeper win!')
  23.         return True
  24.     else:
  25.         print('天哪,球进了!!')
  26.         return False
  27. #开始比赛(提前比赛)
  28. score_keeper=0
  29. score_shoot=0
  30. man=input('Hei,man please tell us your name:')
  31. round_game=eval(input('how many round do you do'))
  32. #判断总分胜负:
  33. def final():
  34.     if score_keeper>score_shoot:
  35.         print('总冠军:伟大的守门员!!')
  36.     elif score_keeper<score_shoot:
  37.         print('总冠军:%s 你赢啦!'%man)
  38.     else:
  39.         print('平手!决胜局来啦')
  40.         shootdic=shoot(man)
  41.         keeperdic=keeper()
  42.         res=fight(keeperdic,shootdic)
  43.         if res:
  44.             print('Everybody!!Winner is goalkeeper')
  45.         else:
  46.             print('Everybody!!Winner is %s'%man)
  47.    
  48. #开始比赛
  49. for i in range(round_game):
  50.         print('====Everybody the %dth round begin!===='%(i+1))
  51.         shootdic=shoot(man)
  52.         keeperdic=keeper()
  53.         res=fight(keeperdic,shootdic)
  54.         if res:
  55.             score_keeper=score_keeper+1
  56.             if score_keeper>=round_game//2+1:
  57.                 final()
  58.                 break
  59.         else:
  60.             score_shoot=score_shoot+1
  61.             if score_shoot>=round_game//2+1:
  62.                 final()
  63.                 break
  64.                


  65.    
  66.         
  67.    


  68.               

  69.    
  70.         
复制代码
回复

使用道具 举报

0

主题

2

好友

72

积分

注册会员

Rank: 2

板凳
发表于 2018-4-9 16:40:42 |显示全部楼层
crossin先生 发表于 2018-4-8 22:58
不算滥用吧,确实需要break

好哒,多谢Crossin先生~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-18 06:43 , Processed in 0.033712 second(s), 24 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部