简体   繁体   中英

Python not printing from list

from random import randint


person = ["My friend", "My sister", "Joe mama", "Barack Obama"]
speech = ["said", "shouted", "whispered"]
personSecond = ["he", "she"]
expression = ["likes", "hates", "always thinks about"]
ing = ["playing computer games", "dominating the world"]

w = randint(0,3)
x = randint(0,2)
y = randint(0,2)
z = randint(0,3)

print(person[w], speech[x], end = " ")

if w == '0' or w == '3':
    print(personSecond[0], end = " ")
elif w == '1' or w == '2':
    print(personSecond[1], end = " ")

print(expression[y], ing[z])

I only get the person, speech, expression and ing output. Python doesn't print strings from personSecond list

What did I do wrong?

You can't compare numbers to strings. w is a number. '0' is a string.

if w == 0 or w == 3

or

if w in (0, 3)

or

if not w or w == 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM