VALUE_1 = "Car Registration No"
VALUE_2 = "Car Make"
VALUE_3 = "Car Model"
VALUE_4 = "Car price"
VALUE_5 = "Car cost"
VALUE_6 = "ERROR"
VALUE_7 = "successfully added"
VALUE_8 = "The total value of stock is £"
VALUE_9 = "CAR SALES"
VALUE_10 = "type E Enter new car"
VALUE_11 = "type D Display all cars"
VALUE_12 = "type C Calulate total stock value"
VALUE_13 = "type S Stop"
VALUE_14 = "Option: "
VALUE_15 = "try again"
car_list = []
def car_to_string(car_in):
output_string = VALUE_1 + ": " + car_in[VALUE_1] + "\n"
output_string += VALUE_2 + ": " + car_in[VALUE_2] + "\n"
output_string += VALUE_3 + ": " + car_in[VALUE_3] + "\n"
output_string += VALUE_4 + ": " + car_in[VALUE_4] + "\n"
output_string += VALUE_5 + ": " + car_in[VALUE_5] + "\n\n"
return output_string
def enter_new_car():
car_registration_no = input(VALUE_1 + ": ")
car_make = input(VALUE_2 + ": ")
car_model = input(VALUE_3 + ": ")
car_price = input(VALUE_4 + ": ")
car_cost = input(VALUE_5 + ": ")
car = {VALUE_1:car_registration_no,
VALUE_2:car_make,
VALUE_3:car_model,
VALUE_4:car_price,
VALUE_5:car_cost}
car_list.append(car)
return VALUE_7
def stock_to_string():
output_string = ""
for car in car_list:
output_string += car_to_string(car)
return output_string
def car_stock_value(car):
car_price = float(car[VALUE_4])
car_cost = float(car[VALUE_5])
return car_price - car_cost
def total_stock_value_as_string():
print(VALUE_12 + str(car_stock_value(car)))
def print_choice():
print("\n")
print(VALUE_9 + "\n")
print(VALUE_10)
print(VALUE_11)
print(VALUE_12)
print(VALUE_13 + "\n")
while True:
print_choice()
choice = input(VALUE_14 + "")
output = ""
if choice == "E":
output == enter_new_car()
elif choice == "D":
output == stock_to_string()
elif choice == "C":
output == total_stock_value_as_string()
elif choice == "S":
break
else:
output = VALUE_15
def stock_to_string():
output_string = ""
for car in car_list:
output_string += car_to_string(car)
return output_string
return 是不是应该写道for循环外边?这样for一遍就返回了
def stock_to_string():
output_string = ""
for car in car_list:
output_string += car_to_string(car)
print(output_string)
return output_string
我用这个print输出出来了,在控制台能打出来