以下是一个简单的猜拳游戏的Python代码示例:

import random

def get_user_choice():
    choice = input("请选择:剪刀(s)、石头(r)或者布(p):")
    while choice not in ['s', 'r', 'p']:
        choice = input("请选择正确的选项:剪刀(s)、石头(r)或者布(p):")
    return choice

def get_computer_choice():
    choices = ['s', 'r', 'p']
    return random.choice(choices)

def determine_winner(user_choice, computer_choice):
    if user_choice == computer_choice:
        return "平局"
    elif (user_choice == 's' and computer_choice == 'p') or \
         (user_choice == 'r' and computer_choice == 's') or \
         (user_choice == 'p' and computer_choice == 'r'):
        return "你赢了"
    else:
        return "计算机赢了"

def play_game():
    user_choice = get_user_choice()
    computer_choice = get_computer_choice()
    print(f"你选择了:{user_choice}")
    print(f"计算机选择了:{computer_choice}")
    print(determine_winner(user_choice, computer_choice))

play_game()

运行以上代码,你可以选择剪刀(s)、石头(r)或者布(p),然后计算机会随机选择一个选项,最后会打印出比赛结果

用python写一个猜拳游戏

原文地址: http://www.cveoy.top/t/topic/iwN6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录