【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;
    }
}
相关文章
|
5天前
|
存储 索引 Python
深度解密 Python 列表的实现原理
深度解密 Python 列表的实现原理
33 13
|
2天前
|
开发者 索引 Python
探索Python中的列表推导式
【9月更文挑战第33天】本文通过直观的示例和代码片段,深入浅出地介绍了Python中强大的功能——列表推导式。我们将从基础概念出发,逐步深入到高级应用,最后探讨其性能考量。无论你是初学者还是有一定经验的开发者,都能在这篇文章中找到有价值的信息。
13 8
|
5天前
|
Python
探索Python中的列表推导式
【9月更文挑战第30天】在编程的世界里,简洁和高效总是我们追求的目标。Python的列表推导式正是这样一把利器,它允许我们在一行代码中生成列表,既清晰又高效。本文将深入浅出地介绍列表推导式的基础知识、高级技巧以及如何避免常见的陷阱,让你的代码更加优雅。
|
6天前
|
索引 Python
python列表删除元素
python列表删除元素
14 1
|
9天前
|
Python
Python 选出列表中特定的元素
Python 选出列表中特定的元素
15 3
|
8天前
|
Python
python编写下象棋游戏|4-14
python编写下象棋游戏|4-14
|
9天前
|
Python
探索Python中的列表推导式
在本文中,我们将深入探讨Python中一个强大且灵活的特性——列表推导式。列表推导式是一种简洁而优雅的方法,用于创建和操作列表。它不仅使代码更易读,还能提高开发效率。通过几个示例,我们将展示列表推导式的多种应用,从基本的操作到复杂的组合,帮助读者更好地理解和利用这一工具。
10 1
|
6天前
|
开发者 Python
探索Python中的列表推导式:一种简洁而强大的工具
【9月更文挑战第29天】在Python的编程世界中,代码的简洁性和可读性总是受到高度赞扬。列表推导式(List Comprehension)作为Python的一个特色功能,不仅能够以简洁的方式生成列表,还能提高代码的执行效率。本文将通过直观的例子和分析,带你深入理解列表推导式的魅力所在,并探讨如何在日常编程中有效利用这一工具来简化代码结构,提升开发效率。
|
6天前
|
存储 索引 Python
Python编程的常用数据结构—列表
Python编程的常用数据结构—列表
|
7天前
|
存储 索引 Python
Python编程的常用数据结构—列表 原创
Python编程的常用数据结构—列表 原创
下一篇
无影云桌面