There's no direct equivalent, but you can convert each value you want to display with the format function. See https://docs.python.org/2/library/string.html#format-specification-mini-language for the format specification.
print '{:02x}'.format(myVar)
Answer from Mark Ransom on Stack OverflowThere's no direct equivalent, but you can convert each value you want to display with the format function. See https://docs.python.org/2/library/string.html#format-specification-mini-language for the format specification.
print '{:02x}'.format(myVar)
I used '{:>n}'.format(nbr).
Example:
In [13]: '{:>2}'.format(1)
Out[13]: ' 1'
In [14]: '{:>2}'.format(10)
Out[14]: '10'
Hello, so ive made a programm in c++, and now im asked to make it in python, I have never programmed in python, so im looking for some help to translate it, would appreciate it.
The programm outputs a queen on a chessboard with and X and also puts an X on all fields where she can move, but all others are 0
This is my C++ programm:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
int x, number; // x ir letter jeb burta apakš deklarācija
char letter;
cout << "Koordinatas burts: "; //Burta ievade
cin >> letter;
cout << "Koordinatas cipars: "; // cipara ievade
cin >> number;
cout << endl;
switch (letter) { // pārvērš ievadītos burtus par cipariem, lai varētu ievadīt karalieni
case 'a': x = 1; break;
case 'b': x = 2; break;
case 'c': x = 3; break;
case 'd': x = 4; break;
case 'e': x = 5; break;
case 'f': x = 6; break;
case 'g': x = 7; break;
case 'h': x = 8; break;
}
for (int i = 1; i <= 8; i++) { // Izveido laukumu
for (int k = 1; k <= 8; k++) { // Izveido laukumu
if (x == k || 9 - number == i) // izvada karalieni un horizontālās un vertikālās līnijascout << setw(2) << 'X'; // setw uzstāda laukuma platumu, izvada X,kas ir karaliene un līnijas
else if (abs(number - 9 + i) == abs(x - k)) // Aprēķina diognāles
cout << setw(2) << 'X'; // izvada diognāles
else
cout << setw(2) << '0'; // izvada pārējo laukumu ar 0
} cout << endl; } return 0;
}
I wrote a program for a class that is a loan calculator. For the outputted table I originally tried using \t for formatting, but that didn't work so im using setw() instead. Whenever the interest gets down to the double or single digits, the balance column is thrown off. Apologies for any style issues I'm still learning, thanks!
include <iostream>
include <cmath>
include <iomanip>
using namespace std;
int main()
{
//setprecision for monthlyInt calculation, setting variables
cout << fixed << showpoint << setprecision(6);
double loanAmount, apr, loanTerm;
double interestTotal = 0.0;
int month = 0;
//input for calculations
cout << "Enter the loan amount: ";
cin >> loanAmount;
cout << "Enter the annual percentage rate: ";
cin >> apr;
cout << "Enter the loan term in months: ";
cin >> loanTerm;
cout << endl;
//calculates based on 12th root of annual interest
double monthlyInt = pow(1 + (apr / 100.0), (1.0 / 12.0)) - 1;
//calculates monthly payments
double monthlyPayments = loanAmount * monthlyInt * (pow((1.0 + monthlyInt), (loanTerm))) / (pow((1.0 + monthlyInt), (loanTerm)) - 1.0);
//defining balance remaining before loop
double balanceLeft = loanAmount;
//header for table
cout << "Month" << setw (10) << "Payment" << setw(14) << "Principal" << setw(11) << "Interest" << setw(12) << "Balance\n";
//loop for decrementing until balance left is 0,
while (balanceLeft > 0) {
cout << fixed << showpoint << setprecision(2);
double interest = balanceLeft * monthlyInt;
double principal = monthlyPayments - interest;
balanceLeft = balanceLeft - principal;
interestTotal = interestTotal + interest;
month++;
cout << month << "\t" << "$" << monthlyPayments << right << setw(6) << "$"
<< principal << right << setw(6) << "$" << interest << right << setw(6)
<< "$" << balanceLeft << "\n";
}
//outputs totals
cout << endl;
cout << "Payment every month is $" << monthlyPayments << endl;
cout << "Total payments is $" << monthlyPayments * loanTerm << endl;
cout << "Total interest is $" << interestTotal << endl;
return 0;
}