-1
fname3 = input("Enter the Blue print name: ")
import re
with open (fname3) as file:
    fileText=file.read()
q1,q2,q3 = [ int(n) for n in re.findall(": (\d+)",fileText) ]
p1,p2,p3 = re.findall("(.*):",fileText)
qb=q1+q2
qc=q1+q2+q3

print("This BLUEPRINT CONTAINS--------------|")
print(p1+" Questions: "+q1)

This code above is giving an error of line: print(p1+" Questions: "+q1) but print(p1+" Questions: "+p1) is giving correct output abd also print("q1") but combining them is outputting an error

but gives error print("questions: "+q1) This code opens a txt file which contains the following:

Part A: 12 10*2 = 20
Part B: 6 4*5 = 20
Part C: 5 3*10 = 30
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    _"This code above is giving an error of line . . ."_ - Please post the full, specific traceback you are getting directly into your question. – Christian Dean Mar 17 '19 at 22:59
  • @ChristianDeanI didn't get you, the last line i.e print(p1+" Questions: "+q1) is giving out error –  Mar 17 '19 at 23:02
  • 1
    If you're getting an error, Python is giving you a _traceback_ - a list of errors. Please post those in your question. – Christian Dean Mar 17 '19 at 23:03
  • 2
    Yes, and you should give a [mcve] showing what error. Also google your error messages. – jonrsharpe Mar 17 '19 at 23:03
  • @JackMoody the ide force closes, if not I would have mentioned it. –  Mar 17 '19 at 23:09

4 Answers4

0

You need to convert to a string with str:

print(p1 + " Questions: " + str(q1))

Alternatively, simply use multiple arguments when calling print:

print(p1, "Questions:", q1)

Note that the spaces are added automatically with this method.

iz_
  • 15,923
  • 3
  • 25
  • 40
0

Another way to do this is with something called f-strings (available in Python 3.6+, but the latest version is 3.7):

print (f"{p1} Questions: {q1}")

Notice how there is an f before the quotes (applies to all types of quotes), and any variable you want has to be in {}

Levi Lesches
  • 1,508
  • 1
  • 13
  • 23
  • So the "f" automatically does the type conversion? –  Mar 17 '19 at 23:06
  • 1
    Well, if you understand the `str.format` method, this is basically a shorthand. For example, `print (num.format (","))` will print `num` with commas where appropriate (which might differ outside of America). However, `print (f"{num:,}")` makes it obvious that `num` is what's being printed and anything after the `:` gets passed to `str.format`. Also, it helps to avoid pesky spaces that get forgotten when using `+` as you can see it plainly. [Click for more details](https://realpython.com/python-f-strings/) – Levi Lesches Mar 17 '19 at 23:25
0

The problem is in the types of your variables.

Questions:, p1, p2 and p3 are all of type str.

Conversely, q1, q2 and q3 are of type int.

The print calls work separately because print can convert its arguments to str. However, you are first trying to add two strings (p1 and Questions:) to an int (q2), which is what fails.

Rather than naive addition/concatenation, you should prefer str.format calls:

print('{p} Questions: {q}'.format(p=p1, q=q1))

These make it easier to understand what the string will look like and automatically perform conversion of your arguments.

gmds
  • 19,325
  • 4
  • 32
  • 58
  • the other answer which had been answered first works and since he gave the ans 1st I marked him, I gave an upvote. –  Mar 17 '19 at 23:14
0

Python is strongly typed language. In most cases, it does not do any implicit type conversion. Consider, should "5"+7 be 12 or "57"? How about 7+"5"?

On ambiguous cases like this, Python will simply raise error rather than trying to guess what you meant.

You need to do the type conversion explicitly:

print(p1+" Questions: "+str(q1))

or with Python 3 f-string:

print(f"{p1} Questions: {q1}")

or print function accepts multiple arguments, that will, by default, be separated by space:

print(p1, "Questions:", q1)
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144