【问题标题】:How would I approach the following question? [closed]我将如何处理以下问题? [关闭]
【发布时间】:2019-02-12 10:00:43
【问题描述】:

“假设我们根据以下规则计算一个单词的元音值:

a - 5 分 e - 4 分 我 - 3 分 o - 2 分 u - 1 分”

我做到了:

https://pastebin.com/gDwRxqG7

a = 5
e = 4
i = 3
o = 2
u = 1

input5 = input("enter word")
for letter = a in input5:
 print("+5 points")
for letter = e in input5:
 print("+4 points")
for letter = i in input5:
 print("+3 points")
for letter = o in input5:
 print("+2 points")
for letter = u in input5:
 print("+1 point")

【问题讨论】:

  • 你可以通过编写一些代码来接近它。
  • 嗨,欢迎来到 StackOverflow。你能告诉我们你的方法是什么。您面临哪些错误或障碍是什么?
  • @clmno 谢谢,已编辑。
  • @meowgoesthedog 我现在编辑了它,抱歉我有点新。
  • 现在,你的问题是什么?

标签: python python-3.x if-statement


【解决方案1】:

首先我将假设您需要找到输入的单词的最终分数。

这是我假设的代码:

input5 = input("enter words\n")

score = 0
for letter in input5:
        if letter == 'a':
                score += 5
        elif letter == 'e':
                score += 4
        elif letter == 'i':
                score += 3
        elif letter == 'o':
                score += 2
        elif letter == 'u':
                score += 1

print(score)

如果一个单词中的每个letter 是其中一个元音,您要比较的语法是错误的。它应该是double equal to,如==。单个= 是分配运算符而不是比较器。
如果您需要打印"+5 points""+4 points"等,您可以更改我的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多