-1

你好!我正在为学校编写代码,您必须编写一个将用户输入转换为猪拉丁语的程序。我的代码是成功的,直到我添加了所需的条件语句。我的代码是:

import sys
   while g < 10:  # Start an infinite loop
    
    message = input("Enter message or type stop: ")
    message = message.strip()
    
    if message == 'stop' or 'Stop':
        print('\nProgram terminated'), sys.exit()
    elif message != 'Stop' or 'stop':
        wordList = message.lower().split()
        vowels = ['a', 'e', 'i', 'o', 'u']
        Message_list = []
        eachWord = []
        for word in wordList:
            if word[0] in 'aeiou':  # case where vowel is first
                Message_list.append(word + 'yay')
            else:
                for letter in word:
                    if letter in 'aeiou':
                        Message_list.append(word[word.index(letter):] + word[:word.index(letter)] + 'ay')
                        break

我无法在此处获得正确的缩进(使用 while 语句),但在我的代码中,缩进是正确的,所以这不是问题。如果我删除,该代码将完美运行:

 if message == 'stop' or 'Stop':
        print('\nProgram terminated'), sys.exit()

但是,当我添加它时,条件总是执行。我不知道问题是什么,因为这是一个有效的条件语句。你们有谁知道发生了什么事吗?

4

1 回答 1

0

这相当于

if (message == 'stop') or 'Stop':

工作选项包括

if message == 'stop' or message == 'Stop':

if message in ('stop', 'Stop'):

if message.upper() == "STOP":
于 2020-10-10T17:09:02.567 回答