#๐ RPG Character Builder
89 messages ยท Page 1 of 1 (latest)
@marble atlas
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.
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.")
sick
Extraordinarily common mistake. So common, we have a command for it lol.
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
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')
@chrome hazel :white_check_mark: Your 3.12 eval job has completed with return code 0.
Bad
btw i seriously appreciate this help .. i know you have better stuff to do :)
!e
choices = ['a', 'b', 'c']
option = 'a' # Actually use 'input' here
if option in choices:
print('Good')
else:
print('Bad')
@chrome hazel :white_check_mark: Your 3.12 eval job has completed with return code 0.
Good
it's difficult trying to go from tutorial mimicing to thinking up a thing and then implementing it
It's 6:30 on a Saturday after drinking a beer and eating dinner. The only better thing I have to do is play Baldur's Gate (which I'm about to).
But yes, this is the hard part. Everyone here that volunteers their time is almost certainly glad they're over that hump. Thankfully, I got over it a decade and a half ago as a teenager.
You really only get better with a lot of practice and banging your head on the wall.
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 ?
Well, the variable is destroyed regardless. All local variables are when the function exits. To return multiple objects though, you can return a tuple:
def procreate():
if random.randint(1, 2) == 1:
return mother_race, father_race
else:
return father_race, mother_race
# Further down
dominant_race, recessive_race = procreate()
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
Like I mentioned before, the variables you create in this function have 0 effect anywhere else. Just return all data, and assign them to variables at the callsite like I showed.
i think i should make classes for the races but im not really sure how to go about it
i think i follow
trying
!e
def func():
some_variable = 1
func()
print(some_variable)
@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
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.
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.
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).
ok
This is the kind of thing that seems stupid until you work on larger systems.
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" ?
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.
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
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.
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
Have you used a while loop?
no
^ is as advanced as i have gotten ... that and using arrays or lists as we have discussed
!e
n = 0
while n < 5:
print(n)
n += 1
@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
Or, something closer to what you want to do:
!e
n = 0
while True:
print(n)
if n > 5:
break
n += 1
@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
Replace the condition in the loop with your "player entered a valid response" check.
Not indexing, no. Just counting up.
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.
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? ')
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")
ok
I'm getting off now. Have a good night.
thanks again
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.