Text-based RPG - character creation problem

This is the place for queries that don't fit in any of the other categories.

Text-based RPG - character creation problem

Postby luis.simao.af98 » Thu May 16, 2013 8:44 pm

Hi all.

I'm new to Python, I've only started since 3 days ago, by viewing some tutorials on youtube and completing codeacademy courses... Yet, I read that the most useful way to learn to code is by coding. So I decided to start making a text-based RPG, like D&D or so... I want it to be good, with some nice features. But I'm stuck in character creation.
Code: Select all
#Divine Dimensions
#A text-based RPG by Luis Ferreira
#Made in Python

print('''You look in front of you and see an old man.
You feel like you\'ve slept for hours and you can\'t remember anything.''')
input()
print('''\"Ha, you\'re finally awake\" - the old man says - \"Who are you?\"''')
input()
print('''
''')
print('''==Character Creation==
''')
print('You\'re now creating your character.')

print('''First, you must chose your race.
Press the name of the race you prefer to see details about it.''')
   
print('- 1 - human')
print('- 2 - elf')
print('- 3 - dwarf')

def race_selection():
    race = input('Chose your race: ')
    print(race)

    if race == human:
        def race1():
            print('Human race desc.')
            var1 = input('Is this your choice (y/n)?: ')
            if var1 == 'y':
                pass
            #add class to continue
            elif var2 == 'n':
                race_selection
        race1()

    elif race == elf:
        def race2():
            print('Elf race desc.')
            var1 = input('Is this your choice (y/n)?: ')
            if var1 == 'y':
                pass
            #add class to continue
            elif var2 == 'n':
                race_selection
        race2()

    elif race == dwarf:
        def race3():
            print('Elf race desc.')
            var1 = input('Is this your choice (y/n)?: ')
            if var1 == 'y':
                pass
            #add class to continue
            elif var2 == 'n':
                race_selection
        race3()

    elif race != 1 and race != 2 and race != 3:
        print('You must chose an available race!')
       
        race_selection()

race_selection()

input()


I have no idea what's wrong, everytime I try to create a new character, it assumes that I haven't chosen an available race... Please, help me with this. Thanks in advance

Best regards,
Luis Ferreira.
luis.simao.af98
 
Posts: 18
Joined: Thu May 16, 2013 8:37 pm

Re: Text-based RPG - character creation problem

Postby dazuza360 » Fri May 17, 2013 1:51 am

When you select 1, 2 or 3 are you getting an error like this:

Code: Select all
Traceback (most recent call last):
  File "C:/Python33/Divine Dimensions.py", line 65, in <module>
    race_selection()
  File "C:/Python33/Divine Dimensions.py", line 27, in race_selection
    if race == human:
NameError: global name 'human' is not defined


This is probably because you haven't actually defined what 'human', 'elf' or 'dwarf' is. When Python goes to the first 'if' statement inside the race_selection, it tries to compare race to human, but doesn't know how because you haven't said what human is yet. I guess you could set human equal to a variable or something.

HINT: Remember that input() will return the string of the user's input, NOT an integer.

I'm still quite new to Python myself so I'm probably not the best person to give advice, but I still hope that makes sense and helps you a bit! If you have other questions I'll be happy to help! :)

-dazuza
dazuza360
 
Posts: 5
Joined: Tue May 14, 2013 10:31 am

Re: Text-based RPG - character creation problem

Postby metulburr » Fri May 17, 2013 2:16 am

i believe you are looking for
Code: Select all
    if race == 'human':

NOTE: you had an unknown variable human and not the string 'human'
or
Code: Select all
    if race == '1':

if you are looking for the user to enter a string number based on whatever choice it is.


Code: Select all
elif race != 1 and race != 2 and race != 3:

use the in operator
Code: Select all
lister = [1,2,3]
if race in lister:
    #do something


Code: Select all
            var1 = input('Is this your choice (y/n)?: ')
            if var1 == 'y':
                pass
            #add class to continue
            elif var2 == 'n':
                race_selection

the fact that you are copy and pasting this code over and over, means you are doing something wrong, well not wrong, but it could bne done better.

Code: Select all
        def race1():

I dont really see the point of a nested defined funciton in this code as you only use it once.

I am not sure if you are aware of this or not, but you make a recursive call to race_selection(), line 64 (maybe line 64, not sure). IT calls itself. This can lead to unexpected results, if you are not careful.

But not bad for 3 days time of learning python
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Text-based RPG - character creation problem

Postby Kebap » Fri May 17, 2013 10:09 am

Hi Luis, welcome to python! For helping, it would be good if you copy also your exact error message, and describe what should happen instead. Happy coding! :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Solved

Postby luis.simao.af98 » Fri May 17, 2013 4:57 pm

Hi all. Thank you very much for all of your help and quick replies.
I actually found out that the problem was far more simple than I thought... As you said, python was trying to compare race to human, for example.
Code: Select all
    if race == human:
        def race1():
            print('Human race desc.')

so, as dazuza and metalburg said:
HINT: Remember that input() will return the string of the user's input, NOT an integer.
I wanted to make race equal to the input, which is a string... the only thing i needed to do was to put human, dwarf and elf inside ' '

Code: Select all
    if race == 'human':
        def race1():
            print('Human race desc.')
            var1 = input('Is this your choice (y/n)?: ')
            if var1 == 'y':
                pass
            #add class to continue
            elif var2 == 'n':
                race_selection
        race1()


so, now it works perfectly and I may continue to work on the code. Once again, thank you very much for your quick replies!

Best regards,
Luis Ferreira.

NOTE: Btw, kebap, thank you for the compliment! I'll keep working and developing my python skills :) Here stays the new code:
Code: Select all
#Divine Dimensions
#A text-based RPG by Luis Ferreira
#Made in Python

print('''You look in front of you and see an old man.
You feel like you\'ve slept for hours and you can\'t remember anything.''')
input()
print('''\"Ha, you\'re finally awake\" - the old man says - \"Who are you?\"''')
input()
print('''
''')
print('''==Character Creation==
''')
print('You\'re now creating your character.')

print('''First, you must chose your race.
Press the name of the race you prefer to see details about it.''')
   
print('- 1 - human')
print('- 2 - elf')
print('- 3 - dwarf')

def race_selection():
    race = input('Chose your race: ')
    print(race)

    if race == 'human':
        def race1():
            print('Human race desc.')
            var1 = input('Is this your choice (y/n)?: ')
            if var1 == 'y':
                pass
            #add class to continue
            elif var2 == 'n':
                race_selection
        race1()

    elif race == 'elf':
        def race2():
            print('Elf race desc.')
            var1 = input('Is this your choice (y/n)?: ')
            if var1 == 'y':
                pass
            #add class to continue
            elif var2 == 'n':
                race_selection
        race2()

    elif race == 'dwarf':
        def race3():
            print('Elf race desc.')
            var1 = input('Is this your choice (y/n)?: ')
            if var1 == 'y':
                pass
            #add class to continue
            elif var2 == 'n':
                race_selection
        race3()

    elif race != 'human' and race != 'elf' and race != 'dwarf':
        print('You must chose an available race!')
       
        race_selection()

race_selection()

input()
luis.simao.af98
 
Posts: 18
Joined: Thu May 16, 2013 8:37 pm

Re: Text-based RPG - character creation problem

Postby dazuza360 » Sun May 19, 2013 5:31 am

I took a look at the new code, and I have a suggestion or two that might help:

Code: Select all
if race == 'human':
        def race1():
            print('Human race desc.')
            var1 = input('Is this your choice (y/n)?: ')
            if var1 == 'y':
                pass
            #add class to continue
            elif var2 == 'n':
                race_selection
        race1()


I think someone else has already mentioned this but you could remove the race1(), race2() and race3() functions coz they are unnecessary if you only used them once.

It's also been pointed out to me by metulburr and some others to use lists (Read metulburr's comment)

Code: Select all
print('''You look in front of you and see an old man.
You feel like you\'ve slept for hours and you can\'t remember anything.''')
input()
print('''\"Ha, you\'re finally awake\" - the old man says - \"Who are you?\"''')
input()


Instead of using the input() function, you could use the time.sleep() function to allow transition to the next sentence or whatever automatically
You use it like this:

Code: Select all
import time

print('This is some random text.')
time.sleep(seconds) # this will temporarily pause the program by the number of seconds in the bracket
print('Some more random text.')


Feel free to have a play around with this but remember to import the module 'time' first (I always forget! :lol: )

Hope that helped you out bro

- dazuza
dazuza360
 
Posts: 5
Joined: Tue May 14, 2013 10:31 am

Re: Text-based RPG - character creation problem

Postby luis.simao.af98 » Mon May 20, 2013 4:29 pm

Wow, awesome! From the tutorials I've followed, I didn't get to know that module (actually, I think I only worked with the math one :P )... Anyway, that'll come really handy! Thank you very much, I really hoped that a module like that existed :)
Thank you once again

Best regards,
Luis Ferreira.
luis.simao.af98
 
Posts: 18
Joined: Thu May 16, 2013 8:37 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 1 guest