[Coursera][Rice] An introduction to interactive Programming in Python Week 1

日期:2014/9/24  --  9/25

Week 0 与 Week 1主要介绍这门课程的课程范围以及教学所使用网站工具,Python基本语法等。

课程所使用在线编程网站 www.codeskulptor.org/  

Notice:

  • def ***:  用来定义函数,若无返回值则默认返回none
  • viz mode 类似于Debug模式,并带有结构对象图
  • Division:/     Integer Division://
  • import:   load a module
  • not、and、or
  • elif (not else if)
  • """  多行注释 “”“

  • Week 0 - Comments, strings, numbers, arithmetic operators, arithmetic expressions, variables
  • Week 1 - Functions, indentation, remainders, modules, Boolean expressions, relational operators, conditional statements
    • Problem:

      • codeskulptor无法进入,需要翻墙。
      • 翻墙之后可以进入网站但是无法加载和保存 Python文件,清理缓存删除相关配置文件依然不行
      • 在进入Mini-Project0时,无法提交,抓狂!后发现已过提交日期,只可以Evaluation
      • 保存Python文件可以通过http://codeskulptor.appspot.com/save/  就能够获得网站文件网址
      • 这个课程感觉老师做的极为用心,从Quizzes到Mini-Project再到为自学准备的Practice Exercises,老师讲话也浅显易懂不看中文字幕完全OK。在做完Mini-project后并不是就这样完了,还需要Evaluation、Self-Evaluation才会出现最终结果。
      • 网站问题最终使用乐飞软件解决,不过免费账户每月只有3G流量。乐飞

      在写Quizzes1时,错了一道题。正确答案应为第一个。



      Mini-project是生活大爆炸上谢耳朵经常玩的那个游戏。看电视剧时压根没明白,现在才搞懂。。。原来是石头剪刀布的升级版ORZ。

      Rock-paper-scissors-lizard-Spock



      根据规则:

      • 4 --> 2
      • 2 --> 0
      • 0 --> 3
      • 3 --> 1
      • 1 --> 4
      • 4 --> 3
      • 3 --> 2
      • 2 --> 1
      • 1 --> 0
      • 0 -->4

      代码就是很简单的判断判断再判断,根据注释一步步写就好了。事实上我的代码写的极为不严谨=。= 

      http://www.codeskulptor.org/#user38_lEdKfkPRoc_0.pyi

          以上为简单的if else语句进行判断,但是提供了一种更为简便的算法,即取模运算即可算出结果。如上图所示,以1为例,对于顺时针的2、3,1都输。对于逆时针的4、1,均赢。即两者相减进行取模,1、2输,3、4赢,0打平。这个方法在最后一个Video有介绍可惜我看时未能看明。白。http://www.codeskulptor.org/#user38_lEdKfkPRoc_3.py
    • 根据这个说法
      diff = (player_number - comp_number) % 5
      才是正确的
      # Rock-paper-scissors-lizard-Spock template
      
      # author:L
      
      import random
      # The key idea of this program is to equate the strings
      # "rock", "paper", "scissors", "lizard", "Spock" to numbers
      # as follows:
      #
      # 0 - rock
      # 1 - Spock
      # 2 - paper
      # 3 - lizard
      # 4 - scissors
      
      # helper functions
      
      def name_to_number(name):
          # delete the following pass statement and fill in your code below
          if name == 'rock':
              return 0
          elif name == 'Spock':
              return 1
          elif name == 'paper':
              return 2
          elif name == 'lizard':
              return 3
          elif name == 'scissors':
              return 4
          else:
              return -1
          # convert name to number using if/elif/else
          # don't forget to return the result!
      
      
      def number_to_name(number):
          # delete the following pass statement and fill in your code below
          if number == 0:
              return 'rock'
          elif number == 1:
              return 'Spock'
          elif number == 2:
              return 'paper'
          elif number == 3:
              return 'lizard'
          elif number == 4:
              return 'scissors'
          else:
              return 'wrong'
          # convert number to a name using if/elif/else
          # don't forget to return the result!
          
      
      def rpsls(player_choice): 
          # delete the following pass statement and fill in your code below
          
          # print a blank line to separate consecutive games
          print ""
          
          # print out the message for the player's choice
          print "Player chooses " + player_choice
          
          # convert the player's choice to player_number using the function name_to_number()
          player_number = name_to_number(player_choice)
          
          # compute random guess for comp_number using random.randrange()
          comp_number = random.randrange(0, 5)
          
          # convert comp_number to comp_choice using the function number_to_name()
          comp_choice = number_to_name(comp_number)
          
          # print out the message for computer's choice
          print "Computer chooses " + comp_choice
      
          # compute difference of comp_number and player_number modulo five
          #diff = (player_number - comp_number + 5) % 5 wrong!!!<pre name="code" class="python">    diff = (player_number - comp_number) % 5<pre name="code" class="python">    # use if/elif/else to determine winner, print winner message
          if diff == 0:
              print "Player and computer tie!"
          elif diff == 1 or diff == 2:
              print  "Computer wins!"
          else:
              print "Player wins!"
              
          # test your code - THESE CALLS MUST BE PRESENT IN YOUR SUBMITTED CODE
          rpsls("rock")
          rpsls("Spock")
          rpsls("paper")
          rpsls("lizard")
          rpsls("scissors")
      
          # always remember to check your completed program against the grading rubric

       
       
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值