Python if的语法无效

Python if的语法无效,python,if-statement,syntax-error,Python,If Statement,Syntax Error,这是一个非常简单的想法,你输入你的考试分数,如果你的分数超过70%(35/50),你可以做1分的修正,基本上给你100%。如果你得到低于70%,你可以做修正1/2点回来 这给了我一个无效的语法,并将光标放在最后一个“和”之间 score=input(“你在测试中答对了多少问题?”) maxscore=50 及格分数=35分 错误=(最大分数-分数) 如果(分数>通行分数): 打印(“您将获得100%”) 如果(分数

这是一个非常简单的想法,你输入你的考试分数,如果你的分数超过70%(35/50),你可以做1分的修正,基本上给你100%。如果你得到低于70%,你可以做修正1/2点回来

这给了我一个无效的语法,并将光标放在最后一个“和”之间

score=input(“你在测试中答对了多少问题?”)
maxscore=50
及格分数=35分
错误=(最大分数-分数)
如果(分数>通行分数):
打印(“您将获得100%”)
如果(分数<通行分数):
打印(“您可以得到“(错误)”%返回文本更正)

我在编程方面很糟糕,如果我在这里显得很愚蠢,那么很抱歉。

你可以用逗号分隔多个参数

print "You can get", wrong, "% back  with text corrections"

在担心语法错误之前,需要将
error
转换为
int
,因为
input()
str
类型返回用户输入

score = int(input("How many problems did you get right on the test?"))
如果没有,并且用户输入了字符串,则表达式:

wrong = (maxscore - score) 
将引发
TypeError
,这本质上意味着您不能从
int
(maxscore)类型的值中减去
str
(score)类型的值


至于你的语法错误

print("You can get"(wrong)"% back  with text corrections")
语法无效。您需要通过在
print()
调用中使用
str()
对其进行转换,将
error
包含为字符串:

print("You can get" + str(wrong) + "% back  with text corrections")

您可以看到,不同类型之间的转换(取决于操作)可能会很混乱,直到挂起为止。

如果要串联字符串,则需要在变量名和字符串之间添加+。将第二行打印替换为:

print("You can get " + str(wrong) + "% back  with text corrections")
问题在于:

print("You can get"(wrong)"% back  with text corrections")
这不是将变量插入字符串的正确方法。您有几个选项:

print("You can get " + str(wrong) + "% back with text corrections")
或:

或:

或:

另外,如果您使用的是Python3,则需要执行
score=int(input(…
)将收到的字符串转换为整数

  • 首先,
    score
    必须是
    int
    。因此需要使用
    int()
    函数来完成此操作
  • 如果不需要(),只需删除它们即可
  • 然后,问题是关于
    打印(“你可以得到“(错误)”%返回文本更正”)
    ,你应该在这里使用
    +
    .format()
    ,等等。记住使用
    str()
    将其转换为字符串

  • 或者像这样:

    print("You can get {0}% back  with text corrections".format(wrong)) 
    
    print("You can get {wrong}% back  with text corrections".format(wrong=wrong)) 
    

    每个人都必须从某个地方开始(我自己对Python还是相当陌生)

    您的第一个问题是需要将
    score
    定义为
    int

    score = int(input("How many problems did you get right on the test?"))
    
    然后,至少有两种解决方案可以修复最后一行代码。一种是使用
    +
    分隔文本字符串,再加上
    str
    错误的
    转换为字符串格式:

    print("You can get " + str(wrong) + "% back  with text corrections")
    
    或者您可以使用
    .format
    方法,这更“Pythonic”:


    您需要将
    错误的
    附加到其两侧的其他两个字符串。使用+或,例如
    打印(“Hello”+“World”)
    相关(但不完全是重复):@TigerhawkT3,因为
    打印(“您将获得100%”)和
    输入()
    函数,我认为OP使用的是Python3。@KevinGuan-从技术上讲,你可以在Python2
    print
    语句中使用括号。类似于
    print('hi')
    的东西在Python2和Python3中产生相同的结果,但是
    print('hi','there')
    产生元组
    ('hi there'))
    在Python 2中,以及字符串
    'hi there'
    在Python 3中。另外,
    input()
    存在于Python 2中,将对输入的字符串求值为
    int
    float
    或其他任何形式-是Python 3需要转换字符串。@KevinGuan-不,这不是
    元组
    。括号只影响分组。
    1,
    元组
    ,而
    (1)
    是一个
    int
    score = int(input("How many problems did you get right on the test?"))
    maxscore = 50
    passscore = 35
    wrong = (maxscore - score)
    
    if score > passscore:
        print("You will get a 100%")
    
    
    if score < passscore:
        print("You can get "+str(wrong)+"% back with text corrections")
    
    print("You can get {0}% back  with text corrections".format(wrong)) 
    
    print("You can get {wrong}% back  with text corrections".format(wrong=wrong)) 
    
    score = int(input("How many problems did you get right on the test?"))
    
    print("You can get " + str(wrong) + "% back  with text corrections")
    
    print("You can get {0}% back  with text corrections".format(wrong))