初学者寻求在python中打印的帮助

时间:2015-10-23 17:29:35

标签: python

public static void numGamesHTWon(String fileName)throws FileNotFoundException{ System.out.print("Number of games the home team won: "); File statsFile = new File(fileName); Scanner input1 = new Scanner(statsFile); String line = input1.nextLine(); Scanner lineScan = new Scanner(line); input1.nextLine(); input1.nextLine(); while (input1.hasNext()) { if (input1.hasNextInt()) { int x = input1.nextInt(); System.out.print(x); input1.next(); } else { input1.next(); } }

我在那里的最后一个引号上一直有语法错误。

无论我做什么样的打印陈述,似乎都不允许我在同一个打印件中放置多个引号。我做错了吗?

2 个答案:

答案 0 :(得分:3)

你错过了一个逗号:

print ("ln(x) at ", x, "is: ", lnx)

答案 1 :(得分:0)

您最好转换到新的打印格式样式:

print('ln(x) at {} is: {}'.format(x, lnx))

使用此表单可以访问Format Specification Mini-Language,它允许您指定宽度,要打印的小数位数等等。

在我打印4位小数的对数时,以下情况很可能会更好:

print('ln({}) = {:,.4f}'.format(x, lnx)

请注意,如果要包含引号,则需要将其转义或将外引号切换到另一组。假设您要打印 Billy“Bobby”Thornton 这两种方式可以做到:

print('Billy "Bobby" Thornton')
print("Billy \"Bobby\" Thornthon")