-1

I am trying to make a random sentence generator, and this piece of code should make the program use "an" when noun2 begins with a vowel.

import random
noun=['it', 'he', 'she', 'I']
verb=['am', 'is', 'are' ]
noun1=random.choice(noun).capitalize()+" "
verb1=random.choice(verb)+" "
noun2=random.choice(noun)+"."
if noun2[:1]=='a' or 'e' or 'i' or 'o' or 'u':
    asentence=noun1+verb1+'an '+noun2
    print(asentence)
else:
  print('hi')

But when I run the program, it always uses "an" even when noun2 starts with a consonant.

F.M
  • 1,369
  • 1
  • 16
  • 31

1 Answers1

1

Python works differently from the english language.

if noun2[:1]=='a' or 'e' or 'i' or 'o' or 'u': should be

if noun2[:1]=='a' or noun2[:1]=='e' or noun2[:1]=='i' or noun2[:1]=='o' or noun2[:1]=='u':

Something like this would be a better way to do it if noun2[:1] in 'aeiou':

This question has been previously answered here. You can read more about logical OR here.

sP_
  • 1,738
  • 2
  • 15
  • 29
  • What does if noun2[:1]=='a' or 'e' or 'i' or 'o' or 'u': do? – F.M Aug 10 '18 at 00:07
  • 1
    @F.M It will always give `True` because all strings (except empty strings) evaluate to True if used as boolean conditions. The above line reads `if "e" or if "i" or if "o"` etc. and they are all `True`. See here for more info: https://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python – berkelem Aug 10 '18 at 03:03