我正在用python做一个加油站项目,有一个错误

2024-09-28 03:13:56 发布

您现在位置:Python中文网/ 问答频道 /正文

print ("Welcome to the Gas Station!")
infiniteLoop = True
total = 0  
while True and infiniteLoop :
   
   print ("\n Gasoline \n Market \n Restaurant \nTotal \n quit to exit")
   select = input("Select One:")
   
   if select == "Gasoline":
       select1 = input("92 - 1 manat \n95 - 1.40 manat \n Diesel - 0.90 cents \n Premium - 2 manat \nWhat Gasoline Do You Want:")
       l = int(input("type liter:"))
       if select1 == "92":
           a = l * 1
           total = total + a
           print(a, "Manat")
           print("Gasoline Injected.")
       elif select1 == "95":
            b = l * 1.40
            total+= b   # update total amount
            b = print(b, "Manat")
            print("Gasoline Injected.")

       elif select1 == "Diesel": # Diesel needs to be String "Diesel"
            c = l * 1.90 
            total = total + c  # update total
            print(c, "Manat")
            print("Gasoline Injected.")
       elif select1 == "Premium": # Premium needs to be sting, in your code has no quote
            d = l * 2.0
            total = total + d
            print(d, "Manat")
            print("Gasoline Injected.")
            
   elif select == "Total":
        print("Total Amount:", total)
    # terminate infinite loop 
   elif select == 'quit':
       infiniteLoop = False

我是Python新手,我正在做一个加油站项目,我还没有完成它,当我在代码中选择一个总数时,它说没有“a”元素,有人能帮忙吗


Tags: totrueinputselecttotalprintelifinjected
3条回答

我认为问题在于,在if/elif语句中声明了变量a、b、c和d。这意味着它们的范围在这些语句中

什么是示波器?这是代码的“区域”,您的变量将被识别为“存在”

例如:

def foo(a) :
    if a is true :
        b = "bar"
    print(b)

在这种情况下,b的作用域在if语句中,if中此赋值之后的所有内容都将访问它。因此,在print(b)调用的时刻,b是未知的,因为我们在范围之外

因此,为了解决问题,您应该在ifs之外定义变量。像这样:

a = 0
b = 0
c = 0
d = 0
if(#####)
    if select1 == 92:
        a = print(l * 1, "Manat")
        print("Gasoline Injected.")
    elif select1 == 95:
        b = print(l * 1.40, "Manat")
        print("Gasoline Injected.")
    elif select1 == Diesel:
        c = print(l * 0.90, "Manat")
        print("Gasoline Injected.")
    elif select1 == Premium:
        d = print(l * 2.0, "Manat")
        print("Gasoline Injected.")
if select == "Total":
    print("Total Amount:", a + b + c + d)

第二点,python中的print函数的返回值是None,这意味着您将None赋值给变量a、b、c、d

因此,要纠正你的a、b、c、d,你应该:

a = l * 1
b = l * 1.40
c = l * 0.90
d = l * 2

两个问题

  • 并非所有的if-elif分支都保证发生在“total”之前,因此,当您尝试打印a+b+c+d时,某些变量可能不存在
  • 您将调用print(...)的结果分配给a, b, c, d,即None

代码中的小问题


a。字符串“Diesel”不等于Diesel

b。压痕

c。可变范围等

不管怎样,这里有一个工作代码,运行它并检查你的代码有什么问题

print ("Welcome to the Gas Station!")
infiniteLoop = True
total = 0  
while True and infiniteLoop :
   
   print ("\n Gasoline \n Market \n Restaurant \nTotal \n quit to exit")
   select = input("Select One:")
   
   if select == "Gasoline":
       select1 = input("92 - 1 manat \n95 - 1.40 manat \n Diesel - 0.90 cents \n Premium - 2 manat \nWhat Gasoline Do You Want:")
       l = int(input("type liter:"))
       if select1 == "92":
           a = l * 1
           total = total + a
           print(a, "Manat")
           print("Gasoline Injected.")
       elif select1 == "95":
            b = l * 1.40
            total+= b   # update total amount
            b = print(b, "Manat")
            print("Gasoline Injected.")

       elif select1 == "Diesel": # Diesel needs to be String "Diesel"
            c = l * 1.90 
            total = total + c  # update total
            print(c, "Manat")
            print("Gasoline Injected.")
       elif select1 == "Premium": # Premium needs to be sting, in your code has no quote
            d = l * 2.0
            total = total + d
            print(d, "Manat")
            print("Gasoline Injected.")
            
   elif select == "Total":
        print("Total Amount:", total)
    # terminate infinite loop 
   elif select == 'quit':
       infiniteLoop = False

相关问题 更多 >

    热门问题