点球大战——python入门练习

功能介绍

我所设置的是默认5回合(回合数可更改)
每回合首先是你从left、center、right三个方向选择一个方向点球,电脑随机选择一个方向防守球,若方向不同,则你得一分,若方向不同,你不得分;
其次是电脑随机选择一个方向点球,你从三个方向选择一个方向防守球,防守成功电脑不得分,否则电脑得一分;
默认开始分数为0:0,比赛结束后会显示你与电脑的比分。

代码部分

goal=[0,0]
from random import choice
direction=['left','center','right']
def kick():
    print('Your turn')
    print('Choose one side to shoot:')
    print('left,center,right')
    you=input()
    print('You kicked '+you)
    com=choice(direction)
    print('Computer saved '+com)
    if you !=com:
        print('Goal!')
        goal[1]+=1
    else:
        print('Oops...')
    print('')
    print('your goal is '+str(goal[1]))
    print('')
def save():
    print("Computer's turn")
    print('Choose one side to save:')
    print('left,center,right')
    you=input()
    print('You saved '+you)
    com=choice(direction)
    print('Computer kicked '+com)
    if you ==com:
        print('Saved!')
    else:
        print('Oops...')
        goal[0]+=1
    print('')
    print('Computer goal is '+str(goal[0]))
    print('')
i=1

#此处的5为5回合,可自己更改回合数
for i in range(5):
    kick()
    save()
else:
    print('your goal is '+str(goal[1]))
    print('Computer goal is '+str(goal[0]))

你可能感兴趣的:(python)