2012-11-16 38 views
0

现在,我正在制作一个程序,以查找在向一组人进行演示时必须购买多少纸张。您必须打印演示文稿的副本。当我去运行程序,它自带了:Python类型错误:尝试打印字符串和整数时出错

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

编辑:我张贴了这个之后,我发现,乔恩·克莱门茨的回答是最好的答案,而且我还发现,我需要把在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

首先&不是concatanation操作(这是按位与运算符) - 这是+,但即使没有一个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+可以省略位置参数,并且只使用{}如果你想)

+0

啊,谢谢,我是相当新到python,我所有的编程历史都在Visual Basic中。另外,感谢您的快速响应。 – Truwinna

相关问题