stduffy06yahoocom
stduffy06yahoocom

Reputation: 11

Input and formatted output

now I just can get the ". " in after the number, I've tried many different ways it just keep showing errors or to much spaces.

current_price = int(input())

last_months_price = int(input())



price_change = current_price - last_months_price

monthly_mortagae = (current_price*0.045)/12

print('This house is','${0}'.format(current_price),'The change is','${0}'.format(price_change),'since last month.')

print('The estimated monthly mortgage is','${}'.format('%0.2f' %monthly_mortagae))

Input

200000

210000

Your output

This house is $200000 The change is $-10000 since last month.

The estimated monthly mortgage is $750.00

Expected output

This house is $200000. The change is $-10000 since last month.

The estimated monthly mortgage is $750.00.

Upvotes: 1

Views: 1291

Answers (2)

Trenton McKinney
Trenton McKinney

Reputation: 62403

Use modern f-strings:

Given:

current_price = 200000
last_months_price = 210000

price_change = current_price - last_months_price
monthly_mortagae = (current_price*0.045)/12

f-strings:

2-lines:

print(f'This house is ${current_price}. The change is ${price_change} since last month.'.replace('$-','-$'))
print(f'The estimated monthly mortgage is ${monthly_mortagae:0.02f}.'.replace('$-','-$'))

1-line:

print(f"""This house is ${current_price}. The change is ${price_change} since last month.
The estimated monthly mortgage is ${monthly_mortagae:0.02f}.""".replace('$-','-$'))
  • Using """some text""" allows for a standard carriage return instead of \n
print("""this
is
a
sentence""")

this
is
a
sentence

Output:

This house is $200000. The change is -$10000 since last month.
The estimated monthly mortgage is $750.00.
  • Using .replace('$-','-$') gets the negative sign to the left of $.

Upvotes: 0

Djaouad
Djaouad

Reputation: 22776

Why not just insert it in the string, and use only one format:

print('This house is ${0}. The change is ${1} since last month.'.format(current_price, price_change))

print('The estimated monthly mortgage is ${:0.2f}.'.format(monthly_mortagae))

With just one print statement:

print('This house is ${0}. The change is ${1} since last month.\nThe estimated monthly mortgage is ${2:0.2f}.'.format(current_price, price_change, monthly_mortagae))

Upvotes: 2

Related Questions