【问题标题】:Newb python score counter trouble with syntax [duplicate]Newb python分数计数器语法问题[重复]
【发布时间】:2018-05-15 17:05:21
【问题描述】:

我正在尝试用 Python 构建一个超级简单的计分器,这是我自学的语言。计数器应该只到 200 并且不超过。 我被困在第二个循环中,并且在第二个条件“> 200”处的第 13 行中出现无效的语法错误。

我还没有完成代码,因为我正在测试每个块。 我可以就收到错误的原因获得帮助吗?此外,我确信这是乱七八糟的代码,但我刚刚学习了函数和参数以及条件,所以这是我目前所知道的。 整个代码如下:

global score #creating starting score
score = 0 # set to 0
global high_score # highest score attainable
high_score = 200 # set to 200
global new_score # name to get current score 
new_score = score + int(input("score: ")) # how current score is reached
def total(): # function to find the complete score
    while score(): # loop for score at zero to add points
        if score is 0:
            print(new_score) #asks user for first score
            break
    while score(): # loop to keep adding numbers
        if new_score < 0 and > 200:
            print("next score: ", + score)
        break

【问题讨论】:

  • and 前后的部分是独立表达式。计算机不懂英语语法,您可以删除代词并知道其他人从上下文中知道您的意思。
  • 啊,所以我必须澄清我的要求。谢谢!
  • 另外,不要使用andnew_score 不能既小于 0 大于 200。你想要 or 在这里。
  • 所以要么new_score &lt; 0 or new_score &gt; 200,要么使用not (0 &lt;= new_score &lt;= 200)
  • 另一个问题:不要在数字上使用is。使用== 来测试,所以if score == 0:score is 0 的工作是实现优化的意外,而不是设计使然。见"is" operator behaves unexpectedly with integers

标签: python-3.x syntax counter


【解决方案1】:
if new_score < 0 and new_score > 200:

顺便说一句,这种情况永远不会发生 :) 你应该重新考虑你的逻辑。

【讨论】:

  • 谢谢,这个和上面的帮助很大,每次我使用“和”时,我都必须在两个表达式中说出变量的名称。
  • 是的,我把它们搞混了。我想我要转储这段代码并为同一件事启动不同的代码。这么多错误,我想我在完全理解函数的工作原理之前就匆忙尝试了。
  • 很高兴我能帮上忙。花一些时间阅读/做一些在线教程。这将为您节省大量时间和挫败感。
  • 我去过。目前我正在阅读“自学成才的程序员”并且完美地跟随。刚刚完成所有基础知识并决定测试自己。但我相信我会找到更多的书,这些书会更深入地了解一切。
猜你喜欢
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
  • 2014-07-13
  • 1970-01-01
  • 2013-01-18
  • 2011-06-04
  • 2013-05-31
相关资源
最近更新 更多