【问题标题】:Assigning Letters To variable将字母分配给变量
【发布时间】:2018-04-18 03:50:40
【问题描述】:

这是我编写的代码,无论分数是多少,输出总是“等级:”,我希望它输出一个等效的字母,但它不会。我尝试过使用逗号,但似乎没有任何效果。我四处张望,但找不到我的问题的答案。

score = input
grade = ""

if score == "5":
    grade == "A"

if score == "4":
    grade == "B"

if score == "3":
    grade == "C"

if score == "2":
    grade == "D"

if score == "1":
    grade = "E"

if score == "0":
    grade == "F"

print("Grade:" + grade)

【问题讨论】:

  • inputscore 实际上是 int 而不是字符串?所以应该是score == 5 而不是score == "5"?抱歉猜测,但没有minimal reproducible example,我只能这样做。
  • score = "5"开头会发生什么?
  • @StefanPochmann 否。示例中input 设置在哪里?它不完整。
  • @StefanPochmann input 这里看起来像一个变量。应该是内置函数吧?
  • @StefanPochmann 哈哈。看来它毕竟是 mcve,而我认为缺少的变量赋值是一个没有被调用的内置函数。我的错。

标签: python variables variable-assignment letter


【解决方案1】:

赋值使用一个等号,比较使用两个。

(分配变量时,只使用一个等号)

【讨论】:

  • 这不起作用我试过了,但感谢您的帮助
  • 我假设您正在某处设置输入变量。不过,您确实需要更改我所说的内容。否则它将无法工作:)
  • 你在设置输入变量吗?如果你是,一切都应该工作。
  • 是的,例如,如果我输入“1”作为分数,则输出为“等级:”
【解决方案2】:

你需要:

1- 使用一个运算符= 进行分配。

2- 通过input()score赋值

score = input("enter the score (0-5):")
grade = ""

if score == "5":
    grade = "A"

if score == "4":
    grade = "B"

if score == "3":
    grade = "C"

if score == "2":
    grade = "D"

if score == "1":
    grade = "E"

if score == "0":
    grade = "F"

print("Grade:" + grade)

【讨论】:

  • 小细节大不同!这就是为什么我不太喜欢 6 个字符的编辑限制。
猜你喜欢
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2016-02-17
  • 2014-05-11
  • 1970-01-01
  • 2019-08-31
  • 1970-01-01
  • 2010-11-17
相关资源
最近更新 更多