i的语法无效

2024-04-26 08:14:59 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个非常简单的想法,你输入你的测试分数,如果你得到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")

我很不擅长编程,如果我在这里看起来真的很蠢的话,我很抱歉。在


Tags: youinputgetif语法分数manyhow
3条回答

可以用逗号分隔多个参数。。在

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

问题在于:

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

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

^{pr2}$

或者:

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='')

另外,如果使用的是python3,则需要执行score = int(input(...操作,将收到的字符串转换为整数。在

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

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

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

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

^{pr2}$

或者您可以使用.format方法,这是更“python”的方法:

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

相关问题 更多 >