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:
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.
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.