3

This is wrecking my brain, and I have searched everywhere to no avail. Here is an example:

masNoun = ["Mann", "Junge"]
femNoun = ["Frau"]
neuNoun = ["Madchen", "Kind"]
masArt = ["ein"]
femArt = ["eine"]
firstProSing = ["Ich"]
seconProSing = ["du"]
thirdProSing = ["Er", "Sie"]
firstVerb = ["bin"]
seconVerb = ["bist"]
thirdVerb = ["ist"]

subject = firstProSing or seconProSing or thirdProSing
verb = firstVerb or seconVerb or thirdVerb
object1 = masArt or femArt
object2 = masNoun or femNoun

Output: Output seems to always be the first variable. I tried using the random module as follows:

object1 = random.choice(masNoun or femNoun)

The result is still the same, with the first variable being the outputted choice. The "or" function seems to not work for my example. I am stumped at this point. Any help would be greatly appreciated!

2
  • 1
    Please explain what you want the result to be. I don't know why you would think that or would do anything randomly. Commented Oct 7, 2015 at 13:32
  • I updated the code above, thanks!
    – kanecain
    Commented Oct 7, 2015 at 13:42

3 Answers 3

2

The or keyword in Python functions as a boolean operator - it will evaluate an expression to True or False if the operands are boolean.

In this case, the operands are not boolean values, so the or operator will return whichever operand evaluates to True. Remember, the or operator works in short-circuit fashion.

For example:

>>> a = 2
>>> b = 3
>>> a or b
2
>>> b or a
3

To me, it looks like you want the variable to take a random value from a given list of possible values.

In that case, what you want is something along these lines:

import random

subjects = ['Ich', 'du', 'er', 'sie']

subject = random.choice(subjects)

EDIT: Per the comment, to do this with multiple lists of variables, instead of one combined list, you can do the following:

import random

firstProSing = ["Ich"]
secondProSing = ["du"]
thirdProSing = ["Er", "Sie"]

subject = random.choice(firstProSing + secondProSing + thirdProSing)

The + operator will take lists and combine them, so this code is the equivalent of:

subjects = firstProSing + secondProSing + thirdProSing
# Comment: Here the content of subjects is ['Ich', 'du', 'Er', 'Sie']
subject = random.choice(subjects)
5
  • Thanks for the response. How can I do this with multiple variables instead of the one? For instance: variable1 = ['Ich', 'du', 'er'] variable2 = ['Mann', 'Frau'] variable3 = random.choice(variable1 or variable2)
    – kanecain
    Commented Oct 7, 2015 at 13:37
  • Cool, I will try that.
    – kanecain
    Commented Oct 7, 2015 at 13:41
  • Works! Thanks for the assistance!
    – kanecain
    Commented Oct 7, 2015 at 13:48
  • 2
    or does not evaluate to True or False; it evaluates to either its first or second argument depending on the truth value of the first argument.
    – chepner
    Commented Oct 7, 2015 at 14:11
  • Good point, I've updated my answer to address this. Please feel free to edit with any corrections! Commented Oct 7, 2015 at 14:17
0

'Or' statement is not the way. It's a boolean, not a random choice. Random choice can be done like this:

import random

subjects = ["Ich", "du", "Er", "Sie"]
verbs = ["bin", "bist", "ist"]
objects1 = ["ein","eine"]
objects2 = ["Mann", "Junge","Frau"]

subject = random.choice(subjects)
verb = random.choice(verbs)
object1 = random.choice(objects1)
object2 = random.choice(objects2)
0
in OR clause if leftmost condition is True python will not look further

firstProSing = ["Ich"]
seconProSing = ["du"]
thirdProSing = ["Er", "Sie"]

subject = firstProSing or seconProSing or thirdProSing

print(firstProSing)

['Ich']

# here the first value is empty or False, python looks
# right and finds that seconProSing is True so its value 'du' is chosen

subject = "" or seconProSing or thirdProSing

print(subject)

['du']
1
  • Please add some explanatory to your answer.
    – kenorb
    Commented Oct 7, 2015 at 15:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.