Text adventure stuff

Question:
Ok go through basic stuff, press R. it needs to input twice. then go through the stuff by taking, it wont send the test message that it worked. How do i fix this?

Repl link:
https://replit.com/@BradyLikes/textadvengine

s = 1
r = 0
c = 0
p = 1
rkey = 0
atk = 5
defense = 2
hp = 10
intel = 4
agi = 1
while True:
  if s == 1:
    print('Welcome to the game, what will your race be?')
    print('Orc: +1ATK (type orc)')
    print('Elf: +1INT (type elf)')
    print('Human: +1HP (type human)')
    print('Dwarf: +1DEF (type dwarf)')
  option = input(">> ")
  if option == ('orc'):
    r = ("Orc")
    atk += 1
    s = 2
  elif option == ('elf'):
    r = ("Elf")
    intel += 1
    s = 2
  elif option == ('human'):
    r = ("Human")
    hp += 1
    s = 2
  elif option == ('dwarf'):
    r = ("Dwarf")
    defense += 1
    s = 2
  if s == 2:
    print('Race: ', r)
    print("What will your class be?")
    print("Wizard: +1INT, -1HP")
    print("Barbarian: +1HP, -1INT")
    print("Fighter: +1ATK, -1DEF")
    print("Rogue: +1AGI, -1DEF")
  option = input(">> ")
  if option == ('wizard'):
    c = ("Wizard")
    s = 3
  elif option == ('barbarian'):
    c = ("Barbarian")
    s = 3
  elif option == ('fighter'):
    c = ("Fighter")
    s = 3
  elif option == ('rogue'):
    c = ("Rogue")
    s = 3
  if s == 3:
    print("Race: ", r)
    print("Class: ", c)
    print("Lets begin.")
    print("")
    print("You enter a town, which way do you go?")
    print("Options: L, R, U, D")
  option = input(">> ")
  if option == ("L"):
    s = 4.1
  if s == 4.1:
    print("You find a coin on the ground, +10G")
    print("Options: leave")
  option = input(">> ")
  if option == ("leave"):
    s == 5
  elif option == ("R"):
    s = 4.2
  if s == 4.2:
    print("You walk through an alley, you find a rusty key")
    print("Options: take, leave")
  option = input(">> ")
  if option == ("take"):
    rkey += 1
    print("You took the key (leave)")
  option = input(">> ")
  if option == ("leave"):
    s == 6
  elif option == ("U"):
    s = 4.3
  elif option == ("D"):
    s = 4.4
  if s == 4.3:
    print("test")
    print("Options: test")
  if s == 4.4:
    print("test")
    print("Options: test")
  if s == 6:
    print("your mom")

link didnt send
https://replit.com/@BradyLikes/textadvengine#main.py

You are not using the operators correctly.

The = is a simple assignment operator. It assigns values from right side operands to the left side operand.

While on the other hand == checks if the values of two operands are equal or not. If yes, the condition becomes true and it returns a non zero value.

So your code would be something like:

s = 1
r = 0
c = 0
p = 1
rkey = 0
atk = 5
defense = 2
hp = 10
intel = 4
agi = 1
while True:
  if s == 1:
    print('Welcome to the game, what will your race be?')
    print('Orc: +1ATK (type orc)')
    print('Elf: +1INT (type elf)')
    print('Human: +1HP (type human)')
    print('Dwarf: +1DEF (type dwarf)')
    option = input(">> ")
    if option == ('orc'):
      r = ("Orc")
      atk += 1
      s = 2
    elif option == ('elf'):
      r = ("Elf")
      intel += 1
      s = 2
    elif option == ('human'):
      r = ("Human")
      hp += 1
      s = 2
    elif option == ('dwarf'):
      r = ("Dwarf")
      defense += 1
      s = 2

#rest of your code
1 Like

Hi there, @BradyLikes!

So your code structure is a bit malformed, which could cause issues down the line, but it looks like you are missing some indents. This should fix the issue you have with needing to take two inputs instead of one.

Old Code
s = 1
r = 0
c = 0
p = 1
rkey = 0
atk = 5
defense = 2
hp = 10
intel = 4
agi = 1
while True:
  if s == 1:
    print('Welcome to the game, what will your race be?')
    print('Orc: +1ATK (type orc)')
    print('Elf: +1INT (type elf)')
    print('Human: +1HP (type human)')
    print('Dwarf: +1DEF (type dwarf)')
  option = input(">> ")
  if option == ('orc'):
    r = ("Orc")
    atk += 1
    s = 2
  elif option == ('elf'):
    r = ("Elf")
    intel += 1
New Code

This should fix your issue, but it may cause unexpected outputs. I wasn’t sure what outcome you wanted to happen when you leave after picking up the key, so I would adjust based off your needs. Also, I would take the user’s input, assign the number, and check what even needed to happen after all the other if and elif statements that assign values to s were finished checking.

s = 1
r = 0
c = 0
p = 1
rkey = 0
atk = 5
defense = 2
hp = 10
intel = 4
agi = 1
while True:
  if s == 1:
    print('Welcome to the game, what will your race be?')
    print('Orc: +1ATK (type orc)')
    print('Elf: +1INT (type elf)')
    print('Human: +1HP (type human)')
    print('Dwarf: +1DEF (type dwarf)')
  option = input(">> ")
  if option == ('orc'):
    r = ("Orc")
    atk += 1
    s = 2
  elif option == ('elf'):
    r = ("Elf")
    intel += 1
    s = 2
  elif option == ('human'):
    r = ("Human")
    hp += 1
    s = 2
  elif option == ('dwarf'):
    r = ("Dwarf")
    defense += 1
    s = 2
  if s == 2:
    print('Race: ', r)
    print("What will your class be?")
    print("Wizard: +1INT, -1HP")
    print("Barbarian: +1HP, -1INT")
    print("Fighter: +1ATK, -1DEF")
    print("Rogue: +1AGI, -1DEF")
  option = input(">> ")
  if option == ('wizard'):
    c = ("Wizard")
    s = 3
  elif option == ('barbarian'):
    c = ("Barbarian")
    s = 3
  elif option == ('fighter'):
    c = ("Fighter")
    s = 3
  elif option == ('rogue'):
    c = ("Rogue")
    s = 3
  if s == 3:
    print("Race: ", r)
    print("Class: ", c)
    print("Lets begin.")
    print("")
    print("You enter a town, which way do you go?")
    print("Options: L, R, U, D")
  option = input(">> ")
  if option == ("L"):
    s = 4.1
  if s == 4.1:
    print("You find a coin on the ground, +10G")
    print("Options: leave")
    option = input(">> ")
    if option == ("leave"):
        s = 5
  elif option == ("R"):
    s = 4.2
  if s == 4.2:
    print("You walk through an alley, you find a rusty key")
    print("Options: take, leave")
    option = input(">> ")
    if option == ("take"):
      rkey += 1
      print("You took the key (leave)")
      option = input(">> ")
      if option == ("leave"):
        s = 6
  elif option == ("U"):
    s = 4.3
  elif option == ("D"):
    s = 4.4
  if s == 4.3:
    print("test")
    print("Options: test")
  if s == 4.4:
    print("test")
    print("Options: test")
  if s == 6:
    print("your mom")

Hope this fixes it!

Edit: It seems like you were using the == operator rather than = when your were assigning values to variables. See @WindLother for an explanation, but I fixed that in your code.

It would be better if you encapsulated your code in a class and replaced the huge amount of if-statements with method calls.

1 Like

This is the way to go, but considering they are a beginner, its probably not worth jumping to that level yet. If @BradyLikes does feel up for learning OOP, I’ll link a tutorial.