If/then loop help

Hey there! I am currently working on an assignment that I got to work as per instructions, but I wanted to modify the program to have another feature that I used in a previous assignment.

My assignment wants me to build a program that reads in a year and outputs the value of the targeted car for that specific year.

My finished code that does work is:

The program works as it’s supposed to, but if a user inputs a year < 1962, the program just ends. I would like it to work similar to this:

where the program asks for a valid input <= 1962.

Thanks!

If you are asking what I think you are asking then you could do something like this:

# Ask for user input and assign to year variable

while year < 1962:
    year = int(input("Please enter a valid year:  "))

# If else tree starting with if 1962 <= year <= 1964

If the year the user puts in is 1962 or greater the while loop won’t run at all and it will just go to the if else.

Yeah, that will work. You’d need to initialize the value of year beforethe loop in this case though.

I tried the submitted code and I got a syntax error were *if 1962 <= year <= 1964.

While I was waiting for a reply, I worked out this:

while True:
  year = int(input("Enter car year:\n"))
  if year < 1962:
    print("Car was not manufactured before 1962")
    continue
  elif year >= 1962:
    print("Great year!")
    break
  elif year <= 1964:
    print("$", 18500)
  elif year <= 1968:
    print("$", 6000)
  elif year <= 1971:
    print("$", 12000)
  elif year <= 1975:
    print("$", 48000)
  elif year <= 1980:
    print("$", 200000)
  elif year <= 1985:
    print("$", 650000)
  elif year <= 2012:
    print("$", 35000000)
  elif year <= 2014:
    print("$", 52000000)

The program loops if the input is the wrong year, but if the right year is input, the program only prints “Gear year!” and stops.

Ideas?

This condition covers a lot of years

This will give you something to think about.

car_values = {"1962" : "Great year!" , "1964" : 18500 , '1968' : 6000 , 
              '1971' : 12000 , '1975' : 48000 , '1980' : 200000 , '1985' : 650000 , 
              '2012' : 35000000 , '2014' : 52000000  }
                                                                                        
                                                                                           
vehicle_years = {'low' : min(car_values.keys()),
                 'high' : max(car_values.keys()), 
                 'sorted' : sorted(car_values.keys())}
                                                                                        
year = 0                                                                                   
message_flag = 0                                                                           
while not(message_flag):                                                                   
    year = input("Enter car year:\n")                                                      
                                                                                           
    if not(year.isdigit()):                                                                
        print("Please try again.")                                                         
    else:                                                                                  
        if int(vehicle_years['low']) <= int(year) <= int(vehicle_years['high']):           
            for val_range in vehicle_years['sorted']:                                      
                if int(val_range) >= int(year):                                            
                    if int(val_range) < 1965:                                              
                        print(car_values['1962']) #TODO: Fix magic numbers                 
                        print(car_values['1964'])                                          
                    else:                                                                  
                        print(car_values[val_range])                                       
                    message_flag = 1                                                       
                    break                                                                  
        else:                                                                              
            print("Enter a value between the years {}-{}".format(vehicle_years['low'],     
                                                                 vehicle_years['high']))