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”元素,有人能帮忙吗
我认为问题在于,在if/elif语句中声明了变量a、b、c和d。这意味着它们的范围在这些语句中
什么是示波器?这是代码的“区域”,您的变量将被识别为“存在”
例如:
在这种情况下,
b
的作用域在if语句中,if中此赋值之后的所有内容都将访问它。因此,在print(b)
调用的时刻,b是未知的,因为我们在范围之外因此,为了解决问题,您应该在ifs之外定义变量。像这样:
第二点,python中的
print
函数的返回值是None
,这意味着您将None
赋值给变量a、b、c、d因此,要纠正你的a、b、c、d,你应该:
两个问题
if-elif
分支都保证发生在“total”之前,因此,当您尝试打印a+b+c+d
时,某些变量可能不存在print(...)
的结果分配给a, b, c, d
,即None
代码中的小问题
a。字符串“Diesel”不等于Diesel
b。压痕
c。可变范围等
不管怎样,这里有一个工作代码,运行它并检查你的代码有什么问题
相关问题 更多 >
编程相关推荐