【发布时间】: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前后的部分是独立表达式。计算机不懂英语语法,您可以删除代词并知道其他人从上下文中知道您的意思。 -
啊,所以我必须澄清我的要求。谢谢!
-
另外,不要使用
and。new_score不能既小于 0 又 大于 200。你想要or在这里。 -
所以要么
new_score < 0 or new_score > 200,要么使用not (0 <= new_score <= 200)。 -
另一个问题:不要在数字上使用
is。使用==来测试值,所以if score == 0:。score is 0的工作是实现优化的意外,而不是设计使然。见"is" operator behaves unexpectedly with integers
标签: python-3.x syntax counter