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!

kanecain
  • 51
  • 5

3 Answers3

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)
Kyle Pittman
  • 2,858
  • 1
  • 30
  • 38
  • 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 Oct 07 '15 at 13:37
  • Cool, I will try that. – kanecain Oct 07 '15 at 13:41
  • Works! Thanks for the assistance! – kanecain Oct 07 '15 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 Oct 07 '15 at 14:11
  • Good point, I've updated my answer to address this. Please feel free to edit with any corrections! – Kyle Pittman Oct 07 '15 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)
grael
  • 657
  • 2
  • 11
  • 26
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']
LetzerWille
  • 5,355
  • 4
  • 23
  • 26