Всякий раз, когда я вводю значение, мне говорят, что переменная не определена. Я почти уверен, что это из-за того, что две функции разделены, но я не знаю, как именно их связать. Пользователь должен ввести только R, S или U для gr и число больше нуля для gal.
Например, если я ввожу S, я получаю это: NameError: имя 'S' не определено
#gas1.py
def main():
gr=eval(input("\nEnter the gas grade. R for Regular, S for Special, U for Super: "))
while (gr!=R or gr!=S or gr!=U):
gr=eval(input("\nGas Grade must be R, S, or U. Enter the gas grade. R for Regular, S for Special, U for Super: "))
gal=eval(input("Enter the number of gallons: "))
while (gal<0):
gal=eval(input("\nNumber of gallons must be greater than zero. Enter the number of gallons: "))
def gasCost(gr, gal):
if gr==R:
cost=2.49*gal
print("You purchased", gal, "gallons of Regular gas at $2.49 per gallon.")
print("Your payment is $", format(cost, "0.2f"))
elif gr==S:
cost=2.79*gal
print("You purchased", gal, "gallons of Special gas at $2.79 per gallon.")
print("Your payment is $", format(cost, "0.2f"))
else:
cost=2.99*gal
print("You purchased", gal, "gallons of Super gas at $2.99 per gallon.")
print("Your payment is $", format(cost, "0.2f"))
main()