-4

I have an assignment that requires me to create a simple character design program that takes in five characteristics and stores them. At the end of the character creation the program needs to ask if the user wants to view, edit, or create a character.

And now I have to create a data structure and the ability to save and load a character and I need a lot of help. I have very little knowledge in programming.

-- Default Character Class

Character = {power = 0, speed = 0, defense = 0, intelligence = 0, stamina = 0}


function Character:create (n)
    n = n or {}
    setmetatable(n, self)
    self.__index = self
    return n
end


-- Function to take user input
function input ()
    print "please enter the power level:"
    local Power = io.read()
    print "please enter the speed level:"
    local Speed = io.read()
    print "please enter the defense level:"
    local Defense = io.read()
    print "please enter the intellegence level:"
    local intelligence = io.read()
    print "please enter the Stamina level:"
    local Stamina = io.read()
end

-- main loop
function MainMenu ()
    selection = {"1", "2", "3" or "4"}
    while selection ~= "1","2", "3" or "4"
        print "1. would you like to edit your character?"
        print "2. would you like to create a new character?"
        print "3. would you like to view a character? "
        print "4. would you like to exit the screen?"

        if selection == "1" then  -- I was uncertain on how to call an already made character or data so I used this method to just recreate a character
            print (character:input)
        elseif selection == "2" then
            print (character:input)
        elseif selection == "3" then
            print (last.character, input()) -- was also unsure on how to code this portion as well.
        elseif selection == "4" then
            io.exit ()
        else selection is ~= selection()
        print "please select from the number below 1-4"

        end
    end
end

update()
menu()
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

2

I don't exactly see a question here. What I see is "Please do my homework for me." StackOverflow isn't a place for you to have other people do your work for you, it's where you ask for help with specific questions.

That being said, I do see a couple small issues:

selection = {"1", "2", "3" or "4"}

Lua is a 'truthy' language. This means that any non-nil value equates to false. Anything else is true. So, when you say "3" or "4", or is a short circuited boolean operator, which means it expects true or false and if the first value is true, it does not check the other operand. "3" is not false, so it must be true. So, in reality, selection is a list containing {"1", "2", "3"}.

Next,

while selection ~= "1","2", "3" or "4"

You cannot perform multiple comparisons like this. While statements loop through a block of code as long as the condition is met. So, I think what you mean to say is something like while selection >= 1 and selection <= 4.

Next,

else selection is ~= selection()

Remove is. Also, selection is not a function, selection is a variable. In Lua, there's not much difference, but in this particular case, you have selection used as a variable. This is why I dislike loosely and dynamically typed languages. It is too easy to lose track of what kind of data you're dealing with.

There are numerous other issues in here. I recommend you email your instructor and ask for his/her help with this. There are a lot of things you seem to have missed. When I was in college, my professors had absolutely little issue with you missing the deadline on an assignment if you talked to them about it the next day. Maybe your professors are the same way.

StephenS
  • 54
  • 2