Liana
Liana

Reputation: 314

printing results in multiple lines in python facing an error

In C++, we simply use std::endl in order to print a result in multiple lines. How can I do the same in Python? I use '\n' and I face a problem.

If I wanted to write the code in C++, here's how I'd write it:

#include <iostream>
#include <math.h>

int main()
{
   int a,b;
   cin>>a>>b;
   cout<<a+b<<endl<<a*b<<endl<<pow(a,b)<<endl;
}

Upvotes: -1

Views: 142

Answers (2)

Grasshopper
Grasshopper

Reputation: 436

#you can use the following format also
print('{}\n{}\n{}\n'.format(num1+num2, num1*num2, num1**num2))

Upvotes: 1

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

Use the sep keyword:

num1 = int(input())
num2 = int(input())

print(num1+num2,num1 * num2,num1**num2, sep='\n')

or simply comma seperate them:

print(num1+num2, '\n', num1 * num2, '\n', num1**num2)

In python + between str and int are not allowed, you could use f-strings if you are running python 3.6+:

print(f"{num1 + num2}\n{num1 * num2}\n{num1**num2}")

Upvotes: 1

Related Questions