【发布时间】:2020-10-31 14:16:33
【问题描述】:
试图让我的第一个程序运行。我试图根据用户输入减去分数。但我的分数不会改变现在的样子。你能帮我理解怎么回事吗?
您可以在下面看到我正在尝试运行的代码:
score = 75
def round_1(score):
shots_1 = input("What did you score: ")
if shots_1 == 1:
return score - 1
elif shots_1 == 2:
return score - 2
elif shots_1 == 3:
return score - 3
else:
return score
【问题讨论】:
-
int(input("What did you score: "))。默认input()返回一个字符串,你需要将它转换为一个int。 -
input()返回一个字符串值,但您正在检查整数值。您的if语句都不为真,因此始终执行else。 -
您必须对
return语句的返回值实际做一些事情 才有意义。向我们展示您调用此函数的代码。 -
用 int() 包装 input() 或使用
if shots_1 == '1'等等 -
@RobinBorg 不要忘记接受答案。