简体   繁体   中英

Python Type Error: Error when trying to print string and integer

Right now, I'm making a program to find how many reams of paper you'd have to buy if you're making a presentation to a group of people and you have to print copies of your presentation. When I go to run the program, it comes up with:

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

What I'm trying to do is:

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

Should I not be using the & operator to combine the string and integer types with print? If not, what am I doing wrong?

Here's my whole program.

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

EDIT: After I posted this I found that Jon Clements answer was the best answer, and I also found that I needed to put in an if statement to make it work. Here's my finished code, thanks to all that helped.

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

Firstly & isn't the concatanation operator (it's the bitwise and operator) - that's + , but even that doesn't work between a str and an int ..., you can use

Python2.x

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

Python 3.x

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

Alternatively, you can use string formatting:

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

(NB: From 2.7+ you can omit the positional parameter, and just use {} if you wanted)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM