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