首页 > 解决方案 > 如何在 Python 中使用间隔制作正确的 IF 语句?

问题描述

[更新的代码进一步下降,存在一个持续的问题]

我最近在我的学校开设了一门完全关于 Python 的课程。从今天开始,我是一个完整的初学者,我刚刚完成了我的第一个任务。

使用 Python 3.10.0

任务如下:假设你是一个买车的商人。一位顾客进来并提供三辆汽车出售。

  1. 收集以下信息。汽车是什么品牌、级别、年份以及行驶了多少英里。
  2. 现在制作一个程序来总结您收集的信息。就我在 Microsoft VSC 中进行调试而言,我已经以自己的方式解决了这个问题,但我认为可能可以以更好的方式完成.. -

尽管任务是根据您使用的函数或变量的数量来评分的,但在这种情况下,老师认为越多越好。所以我想,为什么不尝试根据卖家在区间类型中提供/回答的内容来制定某种价格范围。根据客户提供的有关年份和里程的信息,在这次虚构的对话结束时,将根据给定的信息为所有三辆汽车提供自动评估价格。

所以我的问题是,我在代码末尾使用 IF 语句是否正确,如果是,我做错了什么,或者是另一种解决方案?

此致 :)


q1 = input("What's the first car you'd like to offer? ")
print("Awesome! A " + q1)
q2 = input("What class is it? ")
print("Great.")
q3 = input("Year? ")
q4 = input("And lastly, how many miles have the car driven? ")

q5 = input("What's the second car you'd like to offer? ")
print("Awesome! A " + q5)
q6 = input("What class is it? ")
print("Great.")
q7 = input("Year? ")
q8 = input("And lastly, how many miles have the car driven? ")

q9 = input("What's the third car you'd like to offer? ")
print("Awesome! A " + q9)
q10 = input("What class is it? ")
print("Great.")
q11 = input("Year? ")
q12 = input("And lastly, how many miles have the car driven? ")

#Verbal summary of the offered cars.
print()
print("So we have a " + q1, "model " + q2, "year " + q3, ",that has driven " + q4, "miles.")
print("And a " + q5, "model " + q6, "year " + q7, ",that has driven " + q8, "miles.")
print("Lastly we have the cool " + q9, "model " + q10, "year " + q11, ",that has driven " + q12, "miles.")

#Calculation/Evaluation.
year_question_intervall_type_1 = 1900-1930
year_question_intervall_type_2 = 1931-1950
year_question_intervall_type_3 = 1951-1980
year_question_intervall_type_4 = 1981-2000
year_question_intervall_type_5 = 2001-2021

miles_question_interval_type_1 = 100-1000
miles_question_interval_type_2 = 1001-2000
miles_question_interval_type_3 = 2001-5000
miles_question_interval_type_4 = 5000-8000
miles_question_interval_type_5 = 8001-100000

#Price offer to seller.

if q3 == year_question_intervall_type_1: 
    print("9999 Dollars")

更新的代码问题:#conditions menu_1 如果输入为 1,它会通过触发 def menu_2 和条件来执行我想要的方式。这就是它的结束,它带来了一个结果。不希望之后发生任何其他事情。

但是,如果输入是 2,它应该终止程序,它会不断调出我的 def_menu2。我如何防止这种情况发生?

#car merchant questions to customer
question_1 = input("Merchant: What car do you have to offer? ")
print("Merchant: Alright you got a " + question_1 +".")
question_2 = input("Merchant: What model isit? ")
print("Merchant: Okey..a " + question_2 +".")
question_3 = input("Merchant: Do you happen to know the year model? ")
print("Merchant: Awesome! That's a good year, " + question_3 +".")
question_4 = input("Merchant: Lastly how many kilometers has the car driven? ")
print("Merchant: Thank you. Decent numbers, " + question_4 +".")
print("")
summary_1 = ["Car: " + question_1, "Model: " + question_2, "Year: " + question_3, "Kilometers: " + question_4]
print(summary_1)
print("")

#the first menu of choices for customer
print("Merchant: I have a suggestion. Currently i have four cars available in my garage for exchange. Would you like to see the available cars?")

#menu to the first set of questions
def menu_1(): 
    print("1. Yes")
    print("2. Terminate the program")
menu_1()
option_set_one = int(input("Enter your option: "))
print("")

#conditions menu_1
while option_set_one != 0:
    if option_set_one == 1:
        #prints a list of cars available for exchange
        print("Alright! Lets have a look at my cars.")
        break
    elif option_set_one == 2:
        break
                
#menu_1 condition input 1, provides a list of four cars
cars = ["Porsche Carrera (2002)", "BMW X6 (2015)", "Volvo XC60 (2016)", "Audi A7, 2019"]
if option_set_one == 1:
    print(cars)

#menu_2 condition input 1, provides a menu for the available cars
def menu_2():
    print("1. the Porsche")
    print("2. the BMW")
    print("3. the Volvo")
    print("4. the Audi")
menu_2()
option_set_two = int(input("Which car would you like to exchange your " + question_1 + " with? "))

#conditions menu_2
while option_set_two != 0:
    if option_set_two == 1:
        print("DEAL! The Porsche is yours!")
        break
    elif option_set_two == 2:
        print("DEAL! The BMW is yours!")
        break
    elif option_set_two == 3:
        print("DEAL! The Volvo is yours!")
        break
    elif option_set_two == 4:
        print("DEAL! The Audi is yours!")
        break
    else:
        break       

标签: python

解决方案


不,这不起作用,因为您分配的“间隔”根本不是间隔;它们是简单的整数,因为您所做的只是减法:year_question_intervall_type_1 = 1900-1930将给出-30.

如果要检查间隔,可以将值硬编码到if语句中:

if 1900 <= year <= 1930:
   # do something with this information

但是请注意,您从input()函数中获取的字符串不能直接与数字进行比较。此外,请注意不同的数值比较运算符<<=. 您可能希望它们具有包容性。

为避免对比较值进行硬编码,您可以将它们保存为元组:

year_interval1 = (1900, 1930)

这允许您在任意多个间隔上进行迭代,但它需要更多的代码,这可能超出了您当前任务的范围。


推荐阅读