【python】——使用嵌套列表实现游戏角色管理

简介: 【python】——使用嵌套列表实现游戏角色管理

🎯分析:

本案中,使用嵌套列表实现游戏角色管理,列表元素为字典,每个字典存储一个游戏角色。通过对列表元素(字典)的添加、修改、删除和查询等操作实现对游戏角色的管理。


🎯代码编写:

💛前言:

import sys

print('*' * 40)

print('-----------游戏角色管理-----------')

print('1.查询角色')

print('2.添加角色')

print('3.修改角色')

print('4.删除角色')

print('5.显示所有角色')

print('0.退出')

💛输入判断:

# 角色列表

roleList = [{'姓名':'刘备','武力':7}]

while True:

   x = int(input("请输入您要进行的操作的数字:"))    # 读取数字

   if x in [1,2,3,4,5,0]:


💛查询角色:

if x == 1:    # 查询角色

   姓名 = input("请输入您要查找的姓名")

   for role in roleList:

       if 姓名 == role['姓名']:

           print("姓名:%s,武力:%d"%(role['姓名'],role['武力']))

           break

   else:

       print('抱歉,没有查询到该角色')


💛添加角色:

elif x == 2:    # 添加角色

   姓名 = input('请输入姓名:')

   for role in roleList:

       if 姓名 == role['姓名']:

           print('抱歉,该角色已存在,无法再次添加!')

           break

   else:

       武力 = int(input('请输入对应角色的武力:'))

       newrole = {}

       newrole['姓名'] = 姓名

       newrole['武力'] = 武力

       roleList.append(newrole)


💛修改角色:

elif x == 3:    #修改角色

   姓名 = input("请输入您要修改的姓名:")

   for role in roleList:

       if 姓名 == role['姓名']:

           武力 = int(input('请输入新武力:'))

           role['武力'] = 武力

           print('修改角色成功!')

           break

   else:

       print('该角色不存在!')


💛删除角色:

elif x == 4:

   姓名 = input("请输入您要删除的姓名")

   for role in roleList:

       if 姓名 == role['姓名']:

           roleList.remove(role)

           print('删除成功')

           break

   else:

       print("您要删除的人物不存在")


💛总代码:

import sys
print('*' * 40)
print('-----------游戏角色管理-----------')
print('1.查询角色')
print('2.添加角色')
print('3.修改角色')
print('4.删除角色')
print('5.显示所有角色')
print('0.退出')
# 角色列表
roleList = [{'姓名':'刘备','武力':7}]
while True:
    x = int(input("请输入您要进行的操作的数字:"))    # 读取数字
    if x in [1,2,3,4,5,0]:
        if x == 1:    # 查询角色
            姓名 = input("请输入您要查找的姓名")
            for role in roleList:
                if 姓名 == role['姓名']:
                    print("姓名:%s,武力:%d"%(role['姓名'],role['武力']))
                    break
            else:
                print('抱歉,没有查询到该角色')
        elif x == 2:    # 添加角色
            姓名 = input('请输入姓名:')
            for role in roleList:
                if 姓名 == role['姓名']:
                    print('抱歉,该角色已存在,无法再次添加!')
                    break
            else:
                武力 = int(input('请输入对应角色的武力:'))
                newrole = {}
                newrole['姓名'] = 姓名
                newrole['武力'] = 武力
                roleList.append(newrole)
 
        elif x == 3:    #修改角色
            姓名 = input("请输入您要修改的姓名:")
            for role in roleList:
                if 姓名 == role['姓名']:
                    武力 = int(input('请输入新武力:'))
                    role['武力'] = 武力
                    print('修改角色成功!')
                    break
            else:
                print('该角色不存在!')
        elif x == 4:
            姓名 = input("请输入您要删除的姓名")
            for role in roleList:
                if 姓名 == role['姓名']:
                    roleList.remove(role)
                    print('删除成功')
                    break
            else:
                print("您要删除的人物不存在")
        elif x == 5:
            for role in roleList:
                print("姓名:%s,武力:%d" % (role['姓名'], role['武力']))
        else:
            print("退出程序")
            sys.exit(0)
    else:
        print("输入错误,请重新输入!")


🎯转化为其他程序语言(由chatgpt完成):

🥏C语言:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct Role {
    char name[20];
    int power;
};
 
void printRole(struct Role role) {
    printf("姓名:%s,武力:%d\n", role.name, role.power);
}
 
int main() {
    printf("****************************************\n");
    printf("-----------游戏角色管理-----------\n");
    printf("1.查询角色\n");
    printf("2.添加角色\n");
    printf("3.修改角色\n");
    printf("4.删除角色\n");
    printf("5.显示所有角色\n");
    printf("0.退出\n");
 
    // 角色列表
    struct Role roleList[100];
    strcpy(roleList[0].name, "刘备");
    roleList[0].power = 7;
    int numRoles = 1;
 
    while (1) {
        int x;
        printf("请输入您要进行的操作的数字:");
        scanf("%d", &x);
 
        if (x == 1) {    // 查询角色
            char name[20];
            printf("请输入您要查找的姓名:");
            scanf("%s", name);
 
            int found = 0;
            for (int i = 0; i < numRoles; i++) {
                if (strcmp(name, roleList[i].name) == 0) {
                    printRole(roleList[i]);
                    found = 1;
                    break;
                }
            }
            if (!found) {
                printf("抱歉,没有查询到该角色\n");
            }
 
        } else if (x == 2) {    // 添加角色
            struct Role newRole;
            printf("请输入姓名:");
            scanf("%s", newRole.name);
 
            int found = 0;
            for (int i = 0; i < numRoles; i++) {
                if (strcmp(newRole.name, roleList[i].name) == 0) {
                    printf("抱歉,该角色已存在,无法再次添加!\n");
                    found = 1;
                    break;
                }
            }
 
            if (!found) {
                printf("请输入对应角色的武力:");
                scanf("%d", &newRole.power);
                roleList[numRoles] = newRole;
                numRoles++;
            }
 
        } else if (x == 3) {    // 修改角色
            char name[20];
            printf("请输入您要修改的姓名:");
            scanf("%s", name);
 
            int found = 0;
            for (int i = 0; i < numRoles; i++) {
                if (strcmp(name, roleList[i].name) == 0) {
                    printf("请输入新武力:");
                    scanf("%d", &roleList[i].power);
                    printf("修改角色成功!\n");
                    found = 1;
                    break;
                }
            }
            if (!found) {
                printf("该角色不存在!\n");
            }
 
        } else if (x == 4) {    // 删除角色
            char name[20];
            printf("请输入您要删除的姓名:");
            scanf("%s", name);
 
            int found = 0;
            for (int i = 0; i < numRoles; i++) {
                if (strcmp(name, roleList[i].name) == 0) {
                    for (int j = i; j < numRoles - 1; j++) {
                        roleList[j] = roleList[j + 1];
                    }
                    numRoles--;
                    printf("删除成功\n");
                    found = 1;
                    break;
                }
            }
            if (!found) {
                printf("您要删除的人物不存在\n");
            }
 
        } else if (x == 5) {    // 显示所有角色
            for (int i = 0; i < numRoles; i++) {
                printRole(roleList[i]);
            }
 
        } else if (x == 0) {    // 退出程序
            printf("退出程序\n");
            exit(0);
 
        } else {    // 输入错误
            printf("输入错误,请重新输入!\n");
        }
    }
 
    return 0;
}


💻Java语言:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class GameRoleManagement {
    public static void main(String[] args) {
        System.out.println("****************************************");
        System.out.println("-----------游戏角色管理-----------");
        System.out.println("1.查询角色");
        System.out.println("2.添加角色");
        System.out.println("3.修改角色");
        System.out.println("4.删除角色");
        System.out.println("5.显示所有角色");
        System.out.println("0.退出");
 
        // 角色列表
        List<Role> roleList = new ArrayList<>();
        roleList.add(new Role("刘备", 7));
 
        Scanner scanner = new Scanner(System.in);
 
        while (true) {
            System.out.print("请输入您要进行的操作的数字:");
            int x = scanner.nextInt();
 
            if (x == 1) {    // 查询角色
                System.out.print("请输入您要查找的姓名:");
                String name = scanner.next();
 
                boolean found = false;
                for (Role role : roleList) {
                    if (name.equals(role.getName())) {
                        System.out.printf("姓名:%s, 武力:%d\n", role.getName(), role.getPower());
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    System.out.println("抱歉,没有查询到该角色");
                }
 
            } else if (x == 2) {    // 添加角色
                System.out.print("请输入姓名:");
                String name = scanner.next();
 
                boolean found = false;
                for (Role role : roleList) {
                    if (name.equals(role.getName())) {
                        System.out.println("抱歉,该角色已存在,无法再次添加!");
                        found = true;
                        break;
                    }
                }
 
                if (!found) {
                    System.out.print("请输入对应角色的武力:");
                    int power = scanner.nextInt();
                    Role newRole = new Role(name, power);
                    roleList.add(newRole);
                }
 
            } else if (x == 3) {    // 修改角色
                System.out.print("请输入您要修改的姓名:");
                String name = scanner.next();
 
                boolean found = false;
                for (Role role : roleList) {
                    if (name.equals(role.getName())) {
                        System.out.print("请输入新武力:");
                        int power = scanner.nextInt();
                        role.setPower(power);
                        System.out.println("修改角色成功!");
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    System.out.println("该角色不存在!");
                }
 
            } else if (x == 4) {    // 删除角色
                System.out.print("请输入您要删除的姓名:");
                String name = scanner.next();
 
                boolean found = false;
                for (Role role : roleList) {
                    if (name.equals(role.getName())) {
                        roleList.remove(role);
                        System.out.println("删除成功");
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    System.out.println("您要删除的人物不存在");
                }
 
            } else if (x == 5) {    // 显示所有角色
                for (Role role : roleList) {
                    System.out.printf("姓名:%s, 武力:%d\n", role.getName(), role.getPower());
                }
 
            } else if (x == 0) {    // 退出程序
                System.out.println("退出程序");
                System.exit(0);
 
            } else {    // 输入错误
                System.out.println("输入错误,请重新输入!");
            }
        }
    }
}
 
class Role {
    private String name;
    private int power;
 
    public Role(String name, int power) {
        this.name = name;
        this.power = power;
    }
 
    public String getName() {
        return name;
    }
 
    public int getPower() {
        return power;
    }
 
    public void setPower(int power) {
        this.power = power;
    }
}
相关文章
|
9月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
500 2
|
9月前
|
开发者 Python
Python列表推导式:优雅与效率的完美结合
Python列表推导式:优雅与效率的完美结合
568 116
|
9月前
|
大数据 开发者 Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
503 109
|
9月前
|
Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
583 119
|
9月前
|
Python
Python列表推导式:优雅与效率的艺术
Python列表推导式:优雅与效率的艺术
455 99
|
9月前
|
数据处理 Python
解锁Python列表推导式:优雅与效率的完美融合
解锁Python列表推导式:优雅与效率的完美融合
515 99
|
9月前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
609 95
|
9月前
|
Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
|
9月前
|
索引 Python
Python 列表切片赋值教程:掌握 “移花接木” 式列表修改技巧
本文通过生动的“嫁接”比喻,讲解Python列表切片赋值操作。切片可修改原列表内容,实现头部、尾部或中间元素替换,支持不等长赋值,灵活实现列表结构更新。
462 1

推荐镜像

更多