Python Forum

Full Version: TypeError in my Simple RPG
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is a basic RPG that I'm working on. The program runs fine until the highlighted section. For the most part it runs correctly there, but if you try to enter a number that's not 0, 1, 2, 3, 4, or 5, it crashes. It's supposed to have you re-enter the number if you don't enter a valid one by recalling the function.

import random
import sys

player = {}

#Enemies
goblin = {}
goblin['Name'] = 'Goblin'
goblin['Health'] = 50.0
goblin['Agility'] = 40.0
goblin['Strength'] = 35.0
goblin['Level'] = 1

giant_rat = {}
giant_rat['Name'] = 'Giant Rat'
giant_rat['Health'] = 40.0
giant_rat['Agility'] = 55.0
giant_rat['Strength'] = 30.0
giant_rat['Level'] = 1

feral_dog = {}
feral_dog['Name'] = 'Feral Dog'
feral_dog['Health'] = 50.0
feral_dog['Agility'] = 45.0
feral_dog['Strength'] = 30.0
feral_dog['Level'] = 1

def Death():
    print("You have died!")
    print("GAME OVER")
    print(player)
    player = {}
    exit_game = raw_input()
    main()

def Stats():
    print("Current Stats:")
    print("Current Health: " + str(player['Health']))
    print("Max Health: " + str(player['Healthmax']))
    print("Agility: " + str(player['Agility']))
    print("Strength: " + str(player['Strength']))

def Fight(enemy):
    print(" ")
    print("All of a sudden a " + str(enemy['Name']) + " attacks you!")
    Enemy = enemy.copy()
    while Enemy['Health'] > 0:
        Attack = False
        A = raw_input("Type A to attack the " + str(Enemy['Name']) + ". ")
        if A == "Attack" or A == "attack" or A == "A" or A == "a":
            print("")
            print("You swing at the " + str(Enemy['Name']) + " with your sword!...")
            Player_attack_lands = False
            Enemy_attack_lands = False
            if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0:
                Enemy['Health'] = Enemy['Health'] - player['Strength']
                print("")
                print("You strike the " + str(Enemy['Name']) + " and do " + str(player['Strength']) + " damage!")
                if Enemy['Health'] > 0:
                    print("The " + str(Enemy['Name']) + " now has " + str(Enemy['Health']) + " health left.")
                    print("")
                    print("The " + str(Enemy['Name']) + " launches an attack at you! ")
                    if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0:
                        print("The " + str(Enemy['Name']) + " lands its attack! ")
                        player['Health'] = player['Health'] - Enemy['Strength']
                        print("")
                        print("The " + str(Enemy['Name']) + " deals " + str(Enemy['Strength']) + " damage to you. ")
                        print("You have " + str(player['Health']) + " Health left. ")
                        if player['Health'] <= 0:
                            Death()
                    else:
                        print("The " + str(Enemy['Name']) + " missed! ")
                        print(" ")
            else:
                print("")
                print("You miss!")
                print("The " + str(Enemy['Name']) + " launches an attack at you! ")
                if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0:
                    print("The " + str(Enemy['Name']) + " lands its attack! ")
                    player['Health'] = player['Health'] - Enemy['Strength']
                    print("The " + str(Enemy['Name']) + " deals " + str(Enemy['Strength']) + " damage to you. ")
                    print("You have " + str(player['Health']) + " Health left. ")
                    print(" ")
                    if player['Health'] <= 0:
                        Death()
                else:
                    print("The " + str(Enemy['Name']) + " missed! ")           
    print("")
    print("You've killed the " + str(enemy['Name']) + "!")
    player['Experience'] += Enemy['Level']
    print("You have gained " + str(Enemy['Level']) + " experience points.")
    print("Experience: " + str(player['Experience']) + "/" + str
          (player['Level'] + 9))
    if player['Experience'] >= player['Level'] + 9:
        player['Level'] += 1
        player['Experience'] = 0
        print("You have leveled up!")
        print("")
        raw_input("Press enter to continue. ")
        print("You are now level " + str(player['Level']) + "!")
        print("You have 5 points to spend on your Max Health, Agility, and Strength.")
        print("Your Max Health determines how much damage you can take without dying.")
        print("Your Agility determines how easily you hit your enemies and evade their attacks.")
        print("Your Strength determines how much damage your attacks do.")
        print(" ")
        print(Stats())
        print("")
        def Levelup_healthmax():
            raise_healthmax = float(input("Max Health: "))
            if raise_healthmax != 0 and raise_healthmax != 1 and raise_healthmax != 2 and raise_healthmax != 3 and raise_healthmax != 4 and raise_healthmax != 5:
                raise_healthmax = 0
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                Levelup_healthmax()
            else:
                return raise_healthmax
        def Levelup_agility():
            raise_agility = float(input("Agility: "))
            if raise_agility != 0 and raise_agility != 1 and raise_agility != 2 and raise_agility != 3 and raise_agility != 4 and raise_agility != 5:
                raise_agility = 0
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                Levelup_agility()
            else:
                return raise_agility
        def Levelup_strength():
            raise_strength = float(input("Strength: "))
            if raise_strength != 0 and raise_strength != 1 and raise_strength != 2 and raise_strength != 3 and raise_strength != 4 and raise_strength != 5:
                raise_strength = 0
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                Levelup_strength()
            else:
                return raise_strength
        def Levelup():
            levelup_healthmax = Levelup_healthmax()
            levelup_agility = Levelup_agility()
            levelup_strength = Levelup_strength()
            if levelup_healthmax + levelup_agility + levelup_strength != 5:
                print("You have to spend exactly 5 points on your stats.")
                print(" ")
                Levelup()
            else:
                player['Healthmax'] += levelup_healthmax
                player['Agility'] += levelup_agility
                player['Strength'] += levelup_strength
                player['Health'] = player['Healthmax']
                print(Stats())
        Levelup()

def Character_creation():
    player['Name'] = raw_input("Please enter your character's name. ")
    player['Sex'] = raw_input("Please enter your character's sex. ")
    player['Experience'] = 0
    player['Level'] = 1
    if player['Sex'] == 'female' or player['Sex'] == 'f' or player['Sex'] == 'F':
        player['Sex'] = 'Female'
    elif player['Sex'] == 'male' or player['Sex'] == 'm' or player['Sex'] == 'M':
        player['Sex'] = 'Male'
    else:
        pass
    if player['Sex'] != 'Female' and player['Sex'] != 'Male':
        player['Sex'] = raw_input("Please enter your character's sex again. Please enter Male or Female. ")
    else:
        pass
    player['Strength'] = (random.random() * 100 +1000)
    player['Agility'] = ((random.random() * 100) + 70000) / 2
    player['Health'] = (random.random() * 100 + 10000)
    player['Healthmax'] = player['Health']

def Chapter_01():
    print("You find yourself in a tavern. Around you, people are drinking and conversing with eachother. You sit by the fireplace, which is roaring with heat.")
    print("There are three people next to you that you can talk to: an elderly lady, a young bearded man , and a young blond haired man. Both of the men appear to be in their early twenties.")
    def dialogue_01_function():
        dialogue_01 = raw_input("Type 1 to talk to the lady, 2 to talk to the bearded man, and 3 to talk to the blonde man. ")
        if dialogue_01 == '1':
            print("one")
        elif dialogue_01 == '2':
            print("two")
        elif dialogue_01 == '3':
            print("three")
        else:
            dialogue_01_function()
    dialogue_01_function()

def main():
    Character_creation()
    while player['Health'] > 0:
        shuffle = random.random() * 10
        if shuffle <= 3:
            Fight(goblin)
        elif shuffle >= 7:
            Fight(giant_rat)
        else:
            Fight(feral_dog)
main()
Error:
Traceback (most recent call last): File "C:\Users\Luke\Desktop\Game.py", line 196, in <module> main() File "C:\Users\Luke\Desktop\Game.py", line 195, in main Fight(feral_dog) File "C:\Users\Luke\Desktop\Game.py", line 149, in Fight Levelup() File "C:\Users\Luke\Desktop\Game.py", line 139, in Levelup if levelup_healthmax + levelup_agility + levelup_strength != 5: TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

import random
import sys
 
player = {}
 
#Enemies
goblin = {}
goblin['Name'] = 'Goblin'
goblin['Health'] = 50.0
goblin['Agility'] = 40.0
goblin['Strength'] = 35.0
goblin['Level'] = 1
 
giant_rat = {}
giant_rat['Name'] = 'Giant Rat'
giant_rat['Health'] = 40.0
giant_rat['Agility'] = 55.0
giant_rat['Strength'] = 30.0
giant_rat['Level'] = 1
 
feral_dog = {}
feral_dog['Name'] = 'Feral Dog'
feral_dog['Health'] = 50.0
feral_dog['Agility'] = 45.0
feral_dog['Strength'] = 30.0
feral_dog['Level'] = 1
 
def Death():
    print("You have died!")
    print("GAME OVER")
    print(player)
    player = {}
    exit_game = raw_input()
    main()
 
def Stats():
    print("Current Stats:")
    print("Current Health: " + str(player['Health']))
    print("Max Health: " + str(player['Healthmax']))
    print("Agility: " + str(player['Agility']))
    print("Strength: " + str(player['Strength']))
 
def Fight(enemy):
    print(" ")
    print("All of a sudden a " + str(enemy['Name']) + " attacks you!")
    Enemy = enemy.copy()
    while Enemy['Health'] > 0:
        Attack = False
        A = raw_input("Type A to attack the " + str(Enemy['Name']) + ". ")
        if A == "Attack" or A == "attack" or A == "A" or A == "a":
            print("")
            print("You swing at the " + str(Enemy['Name']) + " with your sword!...")
            Player_attack_lands = False
            Enemy_attack_lands = False
            if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0:
                Enemy['Health'] = Enemy['Health'] - player['Strength']
                print("")
                print("You strike the " + str(Enemy['Name']) + " and do " + str(player['Strength']) + " damage!")
                if Enemy['Health'] > 0:
                    print("The " + str(Enemy['Name']) + " now has " + str(Enemy['Health']) + " health left.")
                    print("")
                    print("The " + str(Enemy['Name']) + " launches an attack at you! ")
                    if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0:
                        print("The " + str(Enemy['Name']) + " lands its attack! ")
                        player['Health'] = player['Health'] - Enemy['Strength']
                        print("")
                        print("The " + str(Enemy['Name']) + " deals " + str(Enemy['Strength']) + " damage to you. ")
                        print("You have " + str(player['Health']) + " Health left. ")
                        if player['Health'] <= 0:
                            Death()
                    else:
                        print("The " + str(Enemy['Name']) + " missed! ")
                        print(" ")
            else:
                print("")
                print("You miss!")
                print("The " + str(Enemy['Name']) + " launches an attack at you! ")
                if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0:
                    print("The " + str(Enemy['Name']) + " lands its attack! ")
                    player['Health'] = player['Health'] - Enemy['Strength']
                    print("The " + str(Enemy['Name']) + " deals " + str(Enemy['Strength']) + " damage to you. ")
                    print("You have " + str(player['Health']) + " Health left. ")
                    print(" ")
                    if player['Health'] <= 0:
                        Death()
                else:
                    print("The " + str(Enemy['Name']) + " missed! ")           
    print("")
    print("You've killed the " + str(enemy['Name']) + "!")
    player['Experience'] += Enemy['Level']
    print("You have gained " + str(Enemy['Level']) + " experience points.")
    print("Experience: " + str(player['Experience']) + "/" + str
          (player['Level'] + 9))
    if player['Experience'] >= player['Level'] + 9:
        player['Level'] += 1
        player['Experience'] = 0
        print("You have leveled up!")
        print("")
        raw_input("Press enter to continue. ")
        print("You are now level " + str(player['Level']) + "!")
        print("You have 5 points to spend on your Max Health, Agility, and Strength.")
        print("Your Max Health determines how much damage you can take without dying.")
        print("Your Agility determines how easily you hit your enemies and evade their attacks.")
        print("Your Strength determines how much damage your attacks do.")
        print(" ")
        print(Stats())
        print("")
        [error]def Levelup_healthmax():
            raise_healthmax = float(input("Max Health: "))
            if raise_healthmax != 0 and raise_healthmax != 1 and raise_healthmax != 2 and raise_healthmax != 3 and raise_healthmax != 4 and raise_healthmax != 5:
                raise_healthmax = 0
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                Levelup_healthmax()
            else:
                return raise_healthmax
        def Levelup_agility():
            raise_agility = float(input("Agility: "))
            if raise_agility != 0 and raise_agility != 1 and raise_agility != 2 and raise_agility != 3 and raise_agility != 4 and raise_agility != 5:
                raise_agility = 0
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                Levelup_agility()
            else:
                return raise_agility
        def Levelup_strength():
            raise_strength = float(input("Strength: "))
            if raise_strength != 0 and raise_strength != 1 and raise_strength != 2 and raise_strength != 3 and raise_strength != 4 and raise_strength != 5:
                raise_strength = 0
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                Levelup_strength()
            else:
                return raise_strength
        def Levelup():
            levelup_healthmax = Levelup_healthmax()
            levelup_agility = Levelup_agility()
            levelup_strength = Levelup_strength()
            if levelup_healthmax + levelup_agility + levelup_strength != 5:
                print("You have to spend exactly 5 points on your stats.")
                print(" ")
                Levelup()
            else:
                player['Healthmax'] += levelup_healthmax
                player['Agility'] += levelup_agility
                player['Strength'] += levelup_strength
                player['Health'] = player['Healthmax']
                print(Stats())
        Levelup()
 
def Character_creation():
    player['Name'] = raw_input("Please enter your character's name. ")
    player['Sex'] = raw_input("Please enter your character's sex. ")
    player['Experience'] = 0
    player['Level'] = 1
    if player['Sex'] == 'female' or player['Sex'] == 'f' or player['Sex'] == 'F':
        player['Sex'] = 'Female'
    elif player['Sex'] == 'male' or player['Sex'] == 'm' or player['Sex'] == 'M':
        player['Sex'] = 'Male'
    else:
        pass
    if player['Sex'] != 'Female' and player['Sex'] != 'Male':
        player['Sex'] = raw_input("Please enter your character's sex again. Please enter Male or Female. ")
    else:
        pass
    player['Strength'] = (random.random() * 100 +1000)
    player['Agility'] = ((random.random() * 100) + 70000) / 2
    player['Health'] = (random.random() * 100 + 10000)
    player['Healthmax'] = player['Health'][/error]
 
def Chapter_01():
    print("You find yourself in a tavern. Around you, people are drinking and conversing with eachother. You sit by the fireplace, which is roaring with heat.")
    print("There are three people next to you that you can talk to: an elderly lady, a young bearded man , and a young blond haired man. Both of the men appear to be in their early twenties.")
    def dialogue_01_function():
        dialogue_01 = raw_input("Type 1 to talk to the lady, 2 to talk to the bearded man, and 3 to talk to the blonde man. ")
        if dialogue_01 == '1':
            print("one")
        elif dialogue_01 == '2':
            print("two")
        elif dialogue_01 == '3':
            print("three")
        else:
            dialogue_01_function()
    dialogue_01_function()
 
def main():
    Character_creation()
    while player['Health'] > 0:
        shuffle = random.random() * 10
        if shuffle <= 3:
            Fight(goblin)
        elif shuffle >= 7:
            Fight(giant_rat)
        else:
            Fight(feral_dog)
main()

        def Levelup_healthmax():
            raise_healthmax = float(input("Max Health: "))
            if raise_healthmax != 0 and raise_healthmax != 1 and raise_healthmax != 2 and raise_healthmax != 3 and raise_healthmax != 4 and raise_healthmax != 5:
                raise_healthmax = 0
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                Levelup_healthmax()
            else:
                return raise_healthmax
        def Levelup_agility():
            raise_agility = float(input("Agility: "))
            if raise_agility != 0 and raise_agility != 1 and raise_agility != 2 and raise_agility != 3 and raise_agility != 4 and raise_agility != 5:
                raise_agility = 0
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                Levelup_agility()
            else:
                return raise_agility
        def Levelup_strength():
            raise_strength = float(input("Strength: "))
            if raise_strength != 0 and raise_strength != 1 and raise_strength != 2 and raise_strength != 3 and raise_strength != 4 and raise_strength != 5:
                raise_strength = 0
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                Levelup_strength()
            else:
                return raise_strength
        def Levelup():
            levelup_healthmax = Levelup_healthmax()
            levelup_agility = Levelup_agility()
            levelup_strength = Levelup_strength()
            if levelup_healthmax + levelup_agility + levelup_strength != 5:
                print("You have to spend exactly 5 points on your stats.")
                print(" ")
                Levelup()
            else:
                player['Healthmax'] += levelup_healthmax
                player['Agility'] += levelup_agility
                player['Strength'] += levelup_strength
                player['Health'] = player['Healthmax']
                print(Stats())
        Levelup()

Ok I fixed it by replacing all the functions with while loops.
        raise_healthmax = 0
        raise_agility = 0
        raise_strength = 0
        n = 0
        while raise_healthmax + raise_agility + raise_strength != 5:
            n += 1
            if n >1:
                print("You have to spend exactly 5 points on your stats.")
            raise_healthmax = float(input("Max Health: "))
            while raise_healthmax != 0 and raise_healthmax != 1 and raise_healthmax != 2 and raise_healthmax != 3 and raise_healthmax != 4 and raise_healthmax != 5:
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                raise_healthmax = float(input("Max Health: "))
                print(" ")
            raise_agility = float(input("Agility: "))
            while raise_agility != 0 and raise_agility != 1 and raise_agility != 2 and raise_agility != 3 and raise_agility != 4 and raise_agility != 5:
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                raise_agility = float(input("Agility: "))
                print(" ")
            raise_strength = float(input("Strength: "))
            while raise_strength != 0 and raise_strength != 1 and raise_strength != 2 and raise_strength != 3 and raise_strength != 4 and raise_strength != 5:
                    print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                    print("")
                    raise_strength = float(input("Strength: "))
                    print(" ")
        player['Healthmax'] += raise_healthmax
        player['Agility'] += raise_agility
        player['Strength'] += raise_strength
        print(Stats())
        print(" ")
1. In the case while loops are better then recursion function

2. Your error with recursion function is you didn't return it.
def Levelup_strength() # should be levelup_strength
    if # code
        return Levelup_string() # you forgot the return
    else:
        return # code
3. small improvements
if raise_agility not in [0,1,2,3,4,5] # instead of typing it out
or
if raise_agility not in range(6)
4. You could have one function.
def raise_stat(title):
    stat = int(input(title))
    while stat not in range(6):
        stat = int(input(title))
        # your code
    return stat