#๐Ÿ”’ RPG Character Builder

89 messages ยท Page 1 of 1 (latest)

signal yokeBOT
#

@marble atlas

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

chrome hazel
#

Your thread got closed before I could answer.

#

!or

signal yokeBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
marble atlas
#

sick

chrome hazel
#

Extraordinarily common mistake. So common, we have a command for it lol.

marble atlas
#

lol

#

i like the last option best anyway

#

learned a new thing

#

ok so let me ask this before i try out a thing

#

what is a good way to display a list of choices for an input, and to only accept those inputs?

i feel like i could do it with if statements, but i also feel like there might be a better way

#

i.e. "what is your mothers race (human, elf, dwarf)?"

#

and huuuman = rejected

#

or trollrace = rejected

chrome hazel
#
choices = ['a', 'b', 'c']
option = 'd'  # Actually use 'input' here
if option in choices:
    print('Good')
else:
    print('Bad')
#

For example

#

!e

choices = ['a', 'b', 'c']
option = 'd'
if option in choices:
    print('Good')
else:
    print('Bad')
signal yokeBOT
#

@chrome hazel :white_check_mark: Your 3.12 eval job has completed with return code 0.

Bad
marble atlas
#

btw i seriously appreciate this help .. i know you have better stuff to do :)

chrome hazel
#

!e

choices = ['a', 'b', 'c']
option = 'a'  # Actually use 'input' here
if option in choices:
    print('Good')
else:
    print('Bad')
signal yokeBOT
#

@chrome hazel :white_check_mark: Your 3.12 eval job has completed with return code 0.

Good
marble atlas
#

it's difficult trying to go from tutorial mimicing to thinking up a thing and then implementing it

chrome hazel
marble atlas
#

hahahahahaha

#

sick

chrome hazel
#

You really only get better with a lot of practice and banging your head on the wall.

marble atlas
#

you said that the procreate function destroys the effective_race variable if i do not return it

#

is there a way to have that set more than one variable?

#

like i want a recessive_race variable too, for whichever parent the player does not "take after" statswise

#
def procreate():
    if random.randint(1, 2) == 1:
        effective_race = mother_race
        return effective_race
    else:
        effective_race = father_race
        return effective_race
#
def procreate():
    if random.randint(1, 2) == 1:
        effective_race = mother_race
        recessive_race = father_race
        return effective_race
    else:
        effective_race = father_race
        recessive_race = mother_race
        return effective_race
#

is recessive_race just destroyed in that ?

chrome hazel
marble atlas
#

i want to keep the variables to use later

#

idk if that makes sense

#

i will eventually have "traits" that get assigned based on parent races

chrome hazel
marble atlas
#

i think i should make classes for the races but im not really sure how to go about it

#

i think i follow

#

trying

chrome hazel
signal yokeBOT
#

@chrome hazel :x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 6, in <module>
003 |     print(some_variable)
004 |           ^^^^^^^^^^^^^
005 | NameError: name 'some_variable' is not defined
chrome hazel
#

The some_variable name is destroyed when the function exits. The same is happening with your effective_race variable inside of the function. The only reason it seems to work is because you have a global variable defined at the bottom with the same name. They're two different variables though.

marble atlas
#

ok that works like a charm

#

i see why

chrome hazel
#

Good. Understanding variable scope is confusing in the beginning, but pretty important.

#

If you wanted to do what you were doing, you could mark effective_race as a global:

def procreate():
    global effective_race
    if random.randint(1, 2) == 1:
        effective_race = mother_race
    else:
        effective_race = father_race

Note the lack of returns.

#

But don't do this when possible. Always use return if the goal of the function is to simply send some object back to the caller.

marble atlas
#

is there a reason for that?

#

*what is the

#

lol

chrome hazel
#

Assigning global variables makes testing harder, and makes tracing data harder. When all functions take input as parameters and give data back using return, it's easy to see where data is coming from and going. When assigning globals within functions, you need to be aware of all the functions thatg may potentially touch the global variable, which makes tracing data increasingly difficult as the program becomes more complex.

#

As soon as you need to debug a complex system, you'll be glad that the system isn't abusing globals (or, if it is abusing globals, you'll see why it's a bad idea).

marble atlas
#

ok

chrome hazel
#

This is the kind of thing that seems stupid until you work on larger systems.

marble atlas
#

for

choices = ['a', 'b', 'c']
option = 'a'  # Actually use 'input' here
if option in choices:
    print('Good')
else:
    print('Bad')

how do i make good = "move on to the next thing" and bad="try that input again" ?

chrome hazel
#

If you want to repeat code, wrap the code in a loop. If you want to leave the loop (because you're done asking), you can use break.

#

A while True loop would be appropriate here.

marble atlas
#
races = ['dwarvan', 'elvan', 'gnoman', 'goblan', 'halvan', 'human', 'northman', 'orcan']
mother_race = input('What race was your mother? ')
    if option in races:
        break
    else:
#

not sure if thats right or what to put in else

chrome hazel
#

Like I mentioned, if you want to repeat code, you'd use a loop. Have you learned about looping yet? I can't remember if I've seen any loops in your code.

marble atlas
#

i did a couple basic for loops in previous iterations of this idea:

for i in parents:
    race = input("What is your " + i + "'s race? (" + str(races) + ')')
    if race == 'Dwarvan' or race == 'dwarvan' or race == 'dwarf':
        print('Your ' + i + ' gave you Dwarvan blood.')
    elif input == 'Elvan' or race == 'elvan' or race == 'elf':
        print('Your ' + i + ' gave you Elvan blood.')
    elif race == 'Gnoman' or race == 'gnoman' or race == 'gnome':
        print('Your ' + i + ' gave you Gnoman blood.')
    elif race == 'Goblan' or race == 'goblan' or race == 'goblin':
        print('Your ' + i + ' gave you Goblan blood.')
    elif race == 'Halvan' or race == 'halvan' or race == 'halfling':
        print('Your ' + i + ' gave you Halvan blood.')
    elif race == 'Human' or race == 'human':
        print('Your ' + i + ' gave you Human blood.')
    elif race == 'Northman' or race == 'northman' or race == 'barbarian':
        print('Your ' + i + ' gave you Northman blood.')
    elif race == 'Orcan' or race == 'orcan' or race == 'orc':
        print('Your ' + i + ' gave you Orcan blood.')
    else:
        print('Please input a race.')

for i in stats:
    total = random.randint(1,4) + random.randint(1,4) + random.randint(1,4)
    print(i+':', total)
#

this time i was trying to make a player character a class first, and someone gave me the neat little roll thing that allows me to XdY

#

you or the guy before

#

i think i have seen it before, but it was the right time for me to learn it today

chrome hazel
#

Have you used a while loop?

marble atlas
#

no

#

^ is as advanced as i have gotten ... that and using arrays or lists as we have discussed

chrome hazel
#

!e

n = 0
while n < 5:
    print(n)
    n += 1
signal yokeBOT
#

@chrome hazel :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 0
002 | 1
003 | 2
004 | 3
005 | 4
marble atlas
#

indexing

#

?

#

n is the index

chrome hazel
#

Or, something closer to what you want to do:

#

!e

n = 0
while True:
    print(n)
    if n > 5:
        break
    n += 1
signal yokeBOT
#

@chrome hazel :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 0
002 | 1
003 | 2
004 | 3
005 | 4
006 | 5
007 | 6
chrome hazel
#

Replace the condition in the loop with your "player entered a valid response" check.

chrome hazel
#

The example was just to show while loops. Your code won't need the number. It will just loop until the user enters a valid response.

#

At which point youbreak.

marble atlas
#

i dont understand how to connect that concept to the list and the input im writing

#
races = ['dwarvan', 'elvan', 'gnoman', 'goblan', 'halvan', 'human', 'northman', 'orcan']
mother_race = input('What race was your mother? ')
chrome hazel
#
choices = ['a', 'b', 'c']
while True:
    option = input('Enter an option')
    if option in choices:
        print('Good')
        break  # Break to quit asking
    else:
        print("Bad. Try Again")
marble atlas
#

ok

chrome hazel
#

I'm getting off now. Have a good night.

marble atlas
#

thanks again

signal yokeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.