基于文本的游戏统计

时间:2021-03-22 20:04:46

标签: python function

我正在做一个小项目,我是为了练习 Python 技能而做的有趣的事情,但我遇到了一个我似乎无法弄清楚的问题。我有一个函数可以为我提供游戏中每个职业的默认统计数据,我希望它根据我在开始时选择的职业/武器而改变。我仍在尝试学习函数,因此任何输入都是有价值的。所以 gameStat() 是默认的统计数据,而 gameStat() 是改变后的数据。

{
    test: /\.(png|jpg|webp|gif|svg|mp4)$/,
    use: [
           {
               loader: 'file-loader',
               options: {
                 name: '[path][name].[ext]?[hash]',
                 context: 'myApp',
                 publicPath: '/myApp',
                 useRelativePath: true,
                 emitFile: false
               }
           }
         ]
  }

非常感谢。

1 个答案:

答案 0 :(得分:0)

当您接受任何输入时,它将以字符串格式与整数进行比较,您需要使用 int() 函数将其转换为整数

我修改了代码,它正确返回了“mana”或“magic”的值

import time
import random
inventory = []

def intro():
    print("Hello for playing my game. It is a text based game that I am working on. \n")
    print("I hope you enjoy playing it \n")

def chooseClass():

    print("Please choose a class you would like to play as. Each class has a different background story to them")
    print("-----------------------------------------------------------")
    print("Please use the number given below to select them")
    print("1: Priest\n" "2: Warrior\n" "3: Thief\n" "4: Ninja\n" "5: Pirate\n")



def classChosen(starterClass):
    if starterClass == 1:
        print("You have chosen Priest")
        return "Priest"
    elif starterClass == 2:
        print("You have chosen Warrior")
        return "Warrior"
    elif starterClass ==3:
        print("You have chosen Thief")
        return "Thief"
    elif starterClass ==4:
        print("You have chosen Ninja")
        return "Ninja"
    elif starterClass ==5:
        print("You have chosen Pirate")
        return "Pirate"
    else:
        return None

def gameStat():
    health = 100
    mana = 100
    strength = 5
    magic = 5
    exp = 0
    baseStats(health,mana,strength,magic,exp)




def ChosenPriest(var):
    if var == "Priest":
        selectAns=""
        selectAns=int(input("Would you like to know the backstory? \n:Please type 1 :Yes or 2 :No\n:"))
        if selectAns == 1:
            print("INC")
            #print("Since a child, you have been serving under the Church of Agathor.\n")
            #print("You didn't have much of a choice because you had nowhere else to go.\n")
            #print("You don't know who your parents are and the only thing the church would tell you that you suddenly appeared in front of the chruch with a gold cross.\n")
            #print("You still have that cross til this day.\n")
            #print("At the age of 16, everyone who serves for the lord will get their own holy weapon.\n")
            #print("The weapon is used to fight off The Shadows. The Shadows are creatures created by the Shadow Lord, Gilmah.\n")
            #print("Since the very beginning, Gilmah would rummaged through the land destorying and pillaging everything he sees.\n")
            #print("One priest was able to seal him for thousands of years but he is soon to be awaken and he'll become stronger than ever.\n")
        else:
            print("Alright!")
def Weapons(weapon):
    if weapon == 1:
        print("You have chosen Magical Book!")
        inventory.append("Magical Book")
        return "Magical Book"
    elif weapon == 2:
        print("You have choosen a staff")
        inventory.append("Staff")
        return "Staff"


def baseStats(character,weapon):
    if character == "Priest":
        if weapon == "Magical Book":
            mana=100
            mana = mana + 50
            return mana
        elif weapon == "Staff":
            magic=5
            magic = magic + 5
            return magic
#intro()
chooseClass()
userClass=None
while True:
    try:
        x=int(input("What class would you like to play?\n:"))
        if x>5 or x<1:
            continue
        else:
            userClass=classChosen(x)
            break
    except NameError:
        continue

character=ChosenPriest(userClass)

weapon=Weapons(int(input("What kind of holy weapon would you like to take? \n 1: Magical Book \n 2: Staff \n Use 1 or 2 to select your weapon! :")))
print(baseStats(userClass,weapon))

在 gameState() 函数中,baseStats 需要 5 个参数,但是当您定义 baseStats 时,它只需要两个参数字符和武器,这很令人困惑。

相关问题