Reputation: 11
Write multiple if statements:
car_year
is 1969
or earlier, print "Few safety features."
1970
or later, print "Probably has seat belts."
1990
or later, print "Probably has antilock brakes."
2000
or later, print "Probably has airbags."
End each phrase with a period and a newline.
For input: 1995
Probably has seat belts.
Probably has antilock brakes.
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')
This is what I was getting originally:
After trying a few solutions I keep getting this:
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
Reputation: 1
Remove \n from the line print(message1+'.''\n') also from the line print(message2+'.''\n')
Upvotes: 0
Reputation: 9377
Solving the task step by step (iterative):
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:
surround operands by spaces to improve readability, for example
instead less=x<1
prefer better = x > 1
inline the strings/variables when only used once, for example
instead message1 = constant; print(constant)
prefer print('constant')
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
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:
message+'.', end=" "
like suggested by Avijeet's answer you can simply specify the whole terminating suffix as required: end='.\n'
(period followed by newline).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
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 \n
s to get a single newline (the one automatically added by print
).
Upvotes: 4
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