【问题标题】:Invalid syntax with ifif 语法无效
【发布时间】:2016-01-11 20:25:11
【问题描述】:

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

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

score = 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"(wrong)"% back  with text corrections")

如果我在这里看起来真的很愚蠢,我很抱歉。

【问题讨论】:

  • 您需要将wrong 附加到其两侧的其他两个字符串中。使用 + 或 ,。例如。 print("Hello" + "World")
  • 相关(虽然不完全是骗局):stackoverflow.com/questions/13945749/…
  • @TigerhawkT3 因为print ("You will get a 100%")input()函数,我认为OP使用的是Python 3。
  • @KevinGuan - 从技术上讲,您可以在 Python 2 print 语句中使用括号。 print ('hi') 之类的东西在 Python 2 和 3 中产生相同的结果,但 print ('hi', 'there') 在 Python 2 中产生元组 ('hi', 'there') 在 Python 3 中产生字符串 'hi there'。此外,input() 存在于 Python 2 中,并将评估将输入的字符串转换为 intfloat 或其他 - Python 3 需要转换字符串。
  • @KevinGuan - 不,那不是tuple。括号仅影响分组。 1,tuple,而 (1)int

标签: python if-statement syntax-error


【解决方案1】:

问题出在这里:

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

这不是将变量插入字符串的正确方法。您有多种选择:

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

或者:

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

或者:

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

或者:

print("You can get ", wrong, "% back with text corrections", sep='')

此外,如果您使用的是 Python 3,则需要执行 score = int(input(... 将收到的字符串转换为整数。

【讨论】:

    【解决方案2】:

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

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

    score = int(input("How many problems did you get right on the test?"))
    

    那么至少有两种解决方案可以修复最后一行代码。一种是使用+ 分隔你的文本字符串,加上strwrong 转换为字符串格式:

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

    或者您可以使用.format 方法,它更“Pythonic”:

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

    【讨论】:

      【解决方案3】:

      您可以使用逗号分隔多个参数..

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

      【讨论】:

        【解决方案4】:
        1. 首先,score 必须是 int。所以你需要使用int() 函数来做到这一点。
        2. if 不需要(),只需删除它们即可。
        3. 那么,问题是关于print("You can get"(wrong)"% back with text corrections"),你应该在这里使用+,.format()等。记住使用str() 将其转换为字符串。

        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")
        

        这是最简单的方法,但是像这样使用.format()会更清楚:

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

        或者像这样:

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

        【讨论】:

          【解决方案5】:

          如果你想连接一个字符串,你需要在变量名和字符串之间添加 +。将您的第二个打印行替换为:

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

          【讨论】:

            【解决方案6】:

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

            score = int(input("How many problems did you get right on the test?"))
            

            如果你不输入并且用户输入一个字符串,表达式:

            wrong = (maxscore - score) 
            

            将引发TypeError,这实质上意味着您不能从int(最大分数)类型的值中减去str(分数)类型的值。


            至于你的语法错误。

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

            在语法上无效。您需要通过在您的print() 调用中使用str() 转换它来将wrong 包含为字符串:

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

            您可以看到,不同类型之间的转换,具体取决于操作,在您掌握它们之前可能会很混乱。

            【讨论】:

              猜你喜欢
              • 2012-01-31
              • 2016-11-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-12-03
              • 2022-11-13
              • 1970-01-01
              • 2016-07-02
              相关资源
              最近更新 更多