简体   繁体   English

Python中的if / else循环问题

[英]Issues with a if/else loop in Python

I am trying to make this Pig Latin translator in Python and it was working well until I tried to downsize it a bit. 我正在尝试使用Python制作此Pig Latin转换器,并且在我尝试缩小尺寸之前一直工作良好。

Can someone please take a look at this code and tell me why, when I type in a word without a vowel at the beginning it will still print the "vowel" code in this if statement? 有人可以看一下这段代码,然后告诉我为什么,当我在开头输入没有元音的单词时,仍然会在此if语句中打印“元音”代码吗?

CODE: 码:

pyg = 'ay'

original = raw_input('Enter a word: ')
low_original = original.lower()

if len(low_original) > 0 and low_original.isalpha():
        print low_original
        if low_original[0] == 'a' or 'e' or 'i' or 'o' or 'u':
                print "vowel"
                pyg_vowel = low_original + pyg
                print pyg_vowel
        else:
                print "consonant"
                pyg_cons = low_original[1: ] + low_original[0] + pyg
                print pyg_cons
else:
        print 'empty'

You need to do a check for all the vowels separately. 您需要分别检查所有元音。

Currently, your if condition is evaluated as: - 目前,您的if条件评估为:-

if (low_original[0] == 'a') or 'e' or 'i' or 'o' or 'u':

or returns the first true value in its condition, which will either True or e here, depending upon your first condition is True or not. or返回其条件中的第一个true值,在这里将为Truee ,具体取决于您的第一个条件为True还是不为True。 Now, since 'e' is evaluated to True , so both the values are true , hence your condition will always be true . 现在,由于'e'被评估为True ,因此两个值都为true ,因此您的条件将始终为true

You should do it like this: - 您应该这样做:-

if low_original[0] in 'aeiou':

or: - 要么: -

if low_original[0] in ('a', 'e', 'i', 'o', 'u'):

You should replace the string: 您应该替换字符串:

if low_original[0] == 'a' or 'e' or 'i' or 'o' or 'u':

with: 与:

if low_original[0] in ('a', 'e', 'i', 'o', 'u'):

The if is alwasys returning True! 如果是alwasys,则返回True!

pyg = 'ay'

original = raw_input('Enter a word: ')
low_original = original.lower()

if len(low_original) > 0 and low_original.isalpha():
    print low_original
    if low_original[0] in ['a' , 'e' , 'i' , 'o' , 'u']:
            print "vowel"
            pyg_vowel = low_original + pyg
            print pyg_vowel
    else:
            print "consonant"
            pyg_cons = low_original[1: ] + low_original[0] + pyg
            print pyg_cons
else:
    print 'empty'

The problem is in 'if low_original[0] == 'a' or 'e' or 'i' or 'o' or 'u':' - first is not pythonic second is not giving you not what you expect. 问题出在'如果low_original [0] =='a'或'e'或'i'或'o'或'u':'-首先不是pythonic其次没有给您没有您期望的结果。

Try to update your code to: 尝试将您的代码更新为:

pyg = 'ay'

original = raw_input('Enter a word: ')
low_original = original.lower()

if len(low_original) > 0 and low_original.isalpha():
    print low_original
    if low_original[0] in ('a', 'e', 'i', 'o', 'u'):
            print "vowel"
            pyg_vowel = low_original + pyg
            print pyg_vowel
    else:
            print "consonant"
            pyg_cons = low_original[1: ] + low_original[0] + pyg
            print pyg_cons
else:
    print 'empty'

将if语句替换为

if low_original[0] in ['a', 'e', 'i', 'o', 'u']

The condition in this if will always evaluate to True . 在这种状况if将始终评估为True

 if low_original[0] == 'a' or 'e' or 'i' or 'o' or 'u':
            print "vowel"
            pyg_vowel = low_original + pyg
            print pyg_vowel

It's the same as if (low_original[0] == 'a') or 'e' or 'i' or 'o' or 'u' if (low_original[0] == 'a') or 'e' or 'i' or 'o' or 'u'

You should use something like if low_original[0] in 'aeiou' 您应该if low_original[0] in 'aeiou'使用if low_original[0] in 'aeiou'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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