【问题标题】:I am having trouble with statements我的陈述有问题
【发布时间】:2020-04-11 13:32:40
【问题描述】:
answer = input ("Type count for counting and convert for converting ")

if (answer == "count"):
    text = input("Paste your text to count it: ")
    print(len(text))

else(answer =="convert"):
    print ("This function is not available yet")

我已尝试使用 elif 和 else,但它在 第 7 行 上不起作用。我无法理解,因为它与我在 youtube 上看到的一样,只是使用不同的字符串/输出文本。它表示 line7 上的单词 else 的语法无效。谁能解释它为什么会发生以及如何解决它?我使用 Pycharm 和最新的 python 版本

【问题讨论】:

  • else 没有条件。使用elif
  • 另外,您似乎在这里缺少右括号: print(len(text) )
  • 我用 elif 也试过同样的地方没有任何区别

标签: if-statement choice multiple-choice


【解决方案1】:

您可以使用 elif 代替 else,也可以编写嵌入的 if 语句(这不是很重要)。

例如 - 如果您使用 elif

answer = input ("Type count for counting and convert for converting ")

if (answer == "count"):
    text = input("Paste your text to count it: ")
    print(len(text))

elif:
    print ("This function is not available yet")

^^与写法基本相同:

answer = input ("Type count for counting and convert for converting ")

if (answer == "count"):
    text = input("Paste your text to count it: ")
    print(len(text))

else:
    if(answer =="convert"):
        print ("This function is not available yet")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    相关资源
    最近更新 更多