【问题标题】:Python Score Invalid Syntax Wont workPython分数无效语法不起作用
【发布时间】:2018-01-23 21:05:17
【问题描述】:
if score <=30:
 print = "You have scored:" + score + ":("

else score > = 31 and < = 60
 print = "You have scored:" + score + ":/"

elif score < = 61
 print = "You have scored:" + score + ":)"

我正在用 python 进行测验,我需要它,以便当分数在 0-30,30-60 和 60+ 之间时,会显示不同的消息,尽管我尝试了上面的代码并且它一直说语法无效.

【问题讨论】:

  • elif score &gt;= 31 and score &lt;= 60 而不是 else score &gt; = 31 and &lt; = 60
  • elif 条件后还需要冒号
  • 它继续说无效的语法
  • 你还需要elif score &gt;= 31 and score &lt;= 60。这是两个独立的条件检查。

标签: python


【解决方案1】:
else score > = 31 and score <= 60

需要

elif score > = 31 and score <= 60:

您还需要在最后一个 elif 中添加 :。而且您的打印语句是错误的。语法为print 'hello'print('hello'),具体取决于python 版本。你真的应该看看初学者 python 语法的东西

【讨论】:

  • @kindall ye 我刚刚注意到原始语法有多大问题...
【解决方案2】:

这是因为 score 变量是一个整数,所以你不能这样打印它。试试这个,你要打印的东西应该放在括号里,而不是“=”。在 if 语句之后也应该有一个 ":" 。试试这个:

if score <=30:
     print("You have scored:" + str(score) + ":(")

elif score >= 31 and score <= 60:
     print("You have scored:" + str(score) + ":/")

elif score <= 61:
     print("You have scored:" + str(score) + ":)")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多