Chinyama
Chinyama

Reputation: 13

Need to have prompts for input in Python

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.

Upvotes: 0

Views: 75

Answers (1)

Matthew Smith
Matthew Smith

Reputation: 510

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: '))

Upvotes: 1

Related Questions