0

现在,我正在编写一个程序来计算如果您要向一群人进行演示并且您必须打印演示文稿的副本,您需要购买多少令纸。当我去运行程序时,它会出现:

Traceback (most recent call last):
File "C:/Users/Shepard/Desktop/Assignment 5.py", line 11, in <module>
print ("Total Sheets: " & total_Sheets & " sheets")
TypeError: unsupported operand type(s) for &: 'str' and 'int'
>>> 

我想做的是:

print ("Total Sheets: " & total_Sheets & " sheets")
print ("Total Reams: " & total_Reams & " reams")

我不应该使用 & 运算符将字符串和整数类型与打印结合起来吗?如果没有,我做错了什么?

这是我的整个程序。

ream = 500
report_Input = int (input ("How many pages long is the report?"))
people_Input = int (input ("How many people do you need to print for? -Automatically prints five extras-"))


people = people_Input + 5

total_Sheets = report_Input * people
total_Reams =((total_Sheets % 500) - total_Sheets) / people

print ("Total Sheets: " & total_Sheets & " sheets")
print ("Total Reams: " & total_Reams & " reams")

编辑:在我发布这个之后,我发现 Jon Clements 的答案是最好的答案,而且我还发现我需要输入一个 if 语句才能使其工作。这是我完成的代码,感谢所有帮助。

ream = 500
report_Input = int (input ("How many pages long is the report?"))
people_Input = int (input ("How many people do you need to print for? -Automatically prints five extras-"))


people = people_Input + 5

total_Sheets = report_Input * people
if total_Sheets % 500 > 0:
    total_Reams =(((total_Sheets - abs(total_Sheets % 500))) / ream)+1
else:
    total_reams = total_Sheets / ream


print ("Total Sheets:", total_Sheets, "sheets")
print ("Total Reams:", total_Reams, "reams")
4

1 回答 1

4

首先&不是串联运算符(它是按位和运算符) - 那是,但即使在 a和...+之间也不起作用,您可以使用strint

Python2.x

print 'Total Sheets:', total_Sheets, 'sheets'

Python 3.x

print ('Total Sheets:', total_Sheets, 'sheets')

或者,您可以使用字符串格式:

print 'Total Sheets: {0} sheets'.format(total_Sheets)

(注意:从 2.7+ 开始,您可以省略位置参数,{}如果需要,只需使用)

于 2012-11-16T19:11:08.010 回答