An Introduction to Interactive Programming in Python 第一周

即将新换电脑为了防止自己所做过的项目丢失,今天开始,就把一些做过的小东西上传到这里,以后也方便查看。

效果图:
在这里插入图片描述

第一次作业比较简单,上代码不多说。

# Rock-paper-scissors-lizard-Spock template


# 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

import random

def name_to_number(guess):
    # delete the following pass statement and fill in your code below
    pass

    # convert name to number using if/elif/else
    # don't forget to return the result!
    if guess == "rock" :
        i=0;
    elif guess == "Spock":
            i=1;
    elif guess == "paper":
            i=2; 
    elif guess == "lizard":
                    i=3;
    elif guess == "scissors":
                        i=4;
    return i
                    

def number_to_name(j):
    # delete the following pass statement and fill in your code below
    pass
  
    # convert number to a name using if/elif/else
    # don't forget to return the result!
    if j == 0 :
        name = "rock";
    elif j== 1:
        name = "Spock";
    elif j == 2:
        name = "paper"; 
    elif j == 3:
        name = "lizard";
    elif j == 4:
        name = "scissors";
    return name 

def rpsls(guess): 
    # delete the following pass statement and fill in your code below
    pass
    
    # print a blank line to separate consecutive games

    # print out the message for the player's choice

    # convert the player's choice to player_number using the function name_to_number()

    # compute random guess for comp_number using random.randrange()

    # convert comp_number to comp_choice using the function number_to_name()
    
    # print out the message for computer's choice

    # compute difference of comp_number and player_number modulo five

    # use if/elif/else to determine winner, print winner message
    print ""
    
    print "Player choose",guess
    
    i = name_to_number(guess)
    
    def random_dice():
        j = random.randrange(0,4)
        return j
    
    j = random_dice()
    name = number_to_name(j)
    print  "Computer choose", name
    
    m = ( i - j ) % 5
    if m == 0:
        print "Player and computer tie!"
    elif  m > 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值