简体   繁体   中英

FIXED If statements with list with python

I'm trying to make a simple "note card" if you can even call it that in Python. I've looked around for hours, I just don't see the problem with my code. I'm trying to randomly choose a word from the string, and if the word is chosen, print the definition. The problem is that the phrase "to be" is going with everything in the list, and not only the [0], which is "ser". Here is my code:

import random

word = ["ser","haber","estar","tener", 'hacer'] 



print(random.choice(word))

for item in word :
    if item == word[0]:
        print("To be")

FIXED CODE

import random

word = ["ser","haber","estar","tener", 'hacer']

choice_word = random.choice(word)
print(choice_word)

if choice_word == word[0]:
        print("To be")

Not sure if this is what you want

import random

word = ["ser","haber","estar","tener", 'hacer'] 

print(random.choice(word))

for item in word :
    print("To be {}".format(item))

I'm not sure that I fully understand your question. But from what I understand is that you want to print To Be when the word[0] "ser" comes up. The issue then, is that you're looping through the list every time your check if item == word[0], so every time it'll print To Be because every time you're loop through the 1st item "ser". Try this instead:

import random

word = ["ser","haber","estar","tener", 'hacer']

choice_word = random.choice(word)
print(choice_word)

if choice_word == word[0]:
        print("To be")

based on what I understood from question, you don't need to iterate over loop before the if statement. Assign the output of random.choice to a variable and then compare it directly

In [123]: x=random.choice(word)

In [124]: if x== word[0]:
     ...:     print("to be")
     ...:

Is this what you are looking for:

import random

word = ["ser","haber","estar","tener", 'hacer']

item = random.choice(word);

print(item)

if item == word[0]:

print("To be")

Try this.

import random

word = ["ser","haber","estar","tener", 'hacer'] 
item = random.choice(word)
if item == word[0]:
    print(item)
    print("To be")

your are not considering your random word

It seems that from you description above, you would like to check the randomly chosen word from the list and check it with the current index in the loop. This is how that'd be done.

import random

word = ["ser","haber","estar","tener", 'hacer']

for item in word :
  choice = random.choice(word)
  print(item, choice)
  if item == choice:
      print("To be")

Your code prints a randomly selected word from the list and then ignores it for the remainder of the code.

Your for loop does the same thing every time you run the program, which is to print "To be" . Why? It checks every word in the list against "ser". The first one will always match, and it will print "To be". In other words, you never used the randomly selected word after you printed it.

At some point, you will wish to print the translation of the randomly chosen word, and you'll learn about dictionaries. Then you can use:

import random
words = {"ser": "To be", "tener": "To have", "saber": "To know"} 
spanish_word = random.choice(list(words.keys()))
print(f'En inglés, {spanish_word} is {words[spanish_word]}')

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