0

I am new to Python, my second week doing it.
I have a code I wrote where one enters year a car was made and it outputs price.
When I enter 1961, I get what I want. When I enter 1962 I get my result.

How can I structure the code such that if I enter any given year period it gives a correct price? Outside the range, it gives an error message. After entering a year and getting a result the user has the option to enter another year or to exit.

year =  int(input('Enter year:\n'))
if year < 1962:
    print('Car did not exist yet!')
    print('Please enter a valid year from 1962'),int(input('Please enter another year:\n'))
if year <= 1964:
    print('$', 18500), int(input('Please enter another year:\n'))
if year >= 1965 <=1968:
    print('$', 6000), int(input('Please enter another year:\n'))
if year >=1969 <=1971:
    print ('$', 12000), int(input('Please enter another year:\n'))

Thank you for any feedback. I tried if/elif to no avail.

Matthew Smith
  • 508
  • 7
  • 22
Chinyama
  • 13
  • 4
  • Use a while loop with a break. There are countless questions here on SO. – Anton vBR May 05 '18 at 07:51
  • Possible duplicate of [how to efficiently check contiguous ranges in python](https://stackoverflow.com/questions/50114208/how-to-efficiently-check-contiguous-ranges-in-python) – fferri May 05 '18 at 07:54

1 Answers1

1

You could use a while loop to overcome this issue. An example of this could include:

year =  int(input('Enter year: '))
while year: # this line forces the program to continue until the user enters no data
   if year < 1962:
      print('Car did not exist yet!')
      print('Please enter a valid year after 1962')
   elif year <= 1964:
      print('$18500')
   elif year >= 1965 <= 1968:
      print('$6000')
   elif year >= 1969 <= 1971:
      print('$12000')
   else:
      print('Error Message') # You can change the error message to display if the year entered is after 1971
   year = int(input('Please enter another year: '))
Matthew Smith
  • 508
  • 7
  • 22
  • Thanks Matthew, I tried the while one before but I guess I did not present it well. Thanks for bringing clarity. I really want to master this. I had also researched on the site and used many books. You guys rock! – Chinyama May 05 '18 at 14:18
  • @Chinyama If you find that this answer helped you, don't forget to mark it as the accepted answer. Happy to help anytime! – Matthew Smith May 07 '18 at 02:14
  • Thanks Matthew, >= 1965 <= 1968 did not work but simply <= 1968 on all upper values worked. Thanks for the guidance. I appreciate your guidance, I am learning this and picking up fast – Chinyama May 08 '18 at 03:10
  • @Chinyama If you find that this answer helped you, don't forget to mark it as the accepted answer and upvote it. Happy to help anytime! – Matthew Smith May 09 '18 at 08:21