Quayshaun Williams
Quayshaun Williams

Reputation: 11

New line and whitespace at the end of a string

Task

Write multiple if statements:

End each phrase with a period and a newline.

Sample output

For input: 1995

Probably has seat belts.
Probably has antilock brakes.

Code

car_year = int(input())

if car_year <=1969:
    message='Few safety features'
    print(message+'.''\n')
if car_year >=1970:
    message1='Probably has seat belts'
    print(message1+'.''\n')
if car_year >=1990:
    message2='Probably has antilock brakes'
    print(message2+'.''\n')
if car_year >=2000:
    message3='Probably has airbags'
    print(message3+'.''\n')

Problem

This is what I was getting originally:

two new-lines after the sentence

After trying a few solutions I keep getting this:

a space after the sentence

Sorry for the terrible explanation. I'm new to coding and don't know how to explain exactly what I'm thinking.

Upvotes: 1

Views: 7735

Answers (4)

Pamela Fields
Pamela Fields

Reputation: 1

Remove \n from the line print(message1+'.''\n') also from the line print(message2+'.''\n')

Upvotes: 0

hc_dev
hc_dev

Reputation: 9377

Solving the task step by step (iterative):

(1) implement all conditional cases (4 if-statements)

car_year = int(input())

if car_year <= 1969:
    print('Few safety features')
if car_year >= 1970:
    print('Probably has seat belts')
if car_year >= 1990:
    print('Probably has antilock brakes')
if car_year >= 2000:
    print('Probably has airbags')

Note some stylistic improvements and simplifications:

  1. surround operands by spaces to improve readability, for example

    instead less=x<1 prefer better = x > 1

  2. inline the strings/variables when only used once, for example

    instead message1 = constant; print(constant) prefer print('constant')

  3. start simple (KISS), for example:

    instead early optimization of format (period plus space at the end) start with print('phrase') and solve formatting in later iteration

(2) implement the formatting (how each phrase should end)

Read the docs about Python's built-in print function:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Print objects to the text stream file, separated by sep and followed by end.

Considerations:

  1. instead message+'.', end=" " like suggested by Avijeet's answer you can simply specify the whole terminating suffix as required: end='.\n' (period followed by newline).
  2. optional: keep it configurable at a central place with a variable phrase_end = '.\n'
phrase_end = '.\n'  # specify the required end

car_year = int(input())

if car_year <= 1969:
    print('Few safety features', end=phrase_end)  # add the argument; for each 
if car_year >= 1970:
    print('Probably has seat belts', end=phrase_end)
if car_year >= 1990:
    print('Probably has antilock brakes', end=phrase_end)
if car_year >= 2000:
    print('Probably has airbags', end=phrase_end)

Upvotes: 1

Jan Pokorn&#253;
Jan Pokorn&#253;

Reputation: 1868

You are adding a newline (\n) to the string, but print automatically adds a single newline after the string, so you get two newlines in total. Just remove the \ns to get a single newline (the one automatically added by print).

Upvotes: 4

asyncg
asyncg

Reputation: 163

You can use the end attribute of the print function for this

car_year = int(input())

if car_year <=1969:
    message='Few safety features'
    print(message+'.', end=" ")
if car_year >=1970:
    message1='Probably has seat belts'
    print(message1+'.', end="")
if car_year >=1990:
    message2='Probably has antilock brakes'
    print(message2+'.', end=" ")
if car_year >=2000:
    message3='Probably has airbags'
    print(message3+'.', end=" ")

Upvotes: 2

Related Questions