在takeUnit1函数中,我试图在TOtbsp函数中访问convFROMconv变量。当我尝试运行它时,我得到:
Traceback (most recent call last):
File "hb.py", line 124, in <module>
takeUnit()
File "hb.py", line 28, in takeUnit1
takeUnit2()
File "hb.py", line 82, in takeUnit2
TOtbsp()
NameError: name 'convFROMconv' is not defined
我已经尝试在声明每个convFROMconv的情况之前添加一个全局声明:
if convFROM == '1':
global convFROMconv
convFROMconv = 'Tablespoons'
elif convFROM == '2':
global convFROMconv
convFROMconv = 'Teaspoons'
elif convFROM == '3':
global convFROMconv
convFROMconv = 'Cups'
elif convFROM == '4':
global convFROMconv
convFROMconv = 'Quarts'
我不确定是否存在与我正在尝试做的事情有关的某种小规则。它甚至可能吗?
import os
tbsp = {'cup' : 0.0625 , 'tsp' : 3 , 'quart' : 0.015625 , 'floz' : 0.5 , 'pint' : 0.03125 , 'gal' : 0.00390625 , 'ml' : 14.7868 , 'liter' : 0.0147868}
tsp = {'cup' : 0.0208333 , 'tbsp' : 0.333333 , 'quart' : 0.0052083333 , 'floz' : 0.1666666667 , 'pint' : 0.0104166667, 'gal' : 0.0013020833 , 'ml' : 4.92892 , 'liter' : 0.00492892}
cup = {'tbsp' : 16 , 'tsp' : 48 , 'quart' : 0.25 , 'floz' : 8.32674 , 'pint' : 0.5 , 'gal' : 0.0625 , 'ml' : 236.588 , 'liter' : 0.236588}
quart = {'cup' : 4 , 'tsp' : 192 , 'tbsp' : 64 , 'floz' : 32 , 'pint' : 2 , 'gal' : 0.25 , 'ml' : 946.353 , 'liter' : 0.946353}
floz = {'cup' : 0.125 , 'tsp' : 6 , 'quart' : 0.03125 , 'tbsp' : 2 , 'pint' : 0.0625 , 'gal' : 0.0078125 , 'ml' : 29.5735 , 'liter' : 0.0295735}
pint = {'cup' : 2 , 'tsp' : 96 , 'quart' : 0.5 , 'floz' : 16 , 'tbsp' : 32 , 'gal' : 0.125 , 'ml' : 473.176 , 'liter' : 0.473176}
gal = {'cup' : 16 , 'tsp' : 768 , 'quart' : 4 , 'floz' : 128 , 'pint' : 8 , 'tbsp' : 256 , 'ml' : 3785.41 , 'liter' : 3.78541}
ml = {'cup' : 0.0042267571 , 'tsp' : 0.202884 , 'quart' : 0.00105669 , 'floz' : 0.033814 , 'pint' : 0.00211338 , 'gal' : 0.000264172 , 'tbsp' : 0.067628 , 'liter' : 0.001}
liter = {'cup' : 4.226757063 , 'tsp' : 202.884 , 'quart' : 1.05669 , 'floz' : 33.814 , 'pint' : 2.11338 , 'gal' : 0.264172 , 'ml' : 1000 , 'tbsp' : 67.628}
acceptableInputs = ['1', '2', '3', '4', '5', '6', '7', '8']
def takeUnit1 ():
print ('Unit Converter')
print ('Select the unit you want to convert FROM')
print ('1 Tbsp - Tablespoon')
print ('2 Tsp - Teaspoon')
print ('3 C - Cups')
print ('4 qt. - Quart')
print ('5 fl. oz. - Fluid Ounce')
print ('6 gal. - Gallon')
print ('7 ml - Milliliter')
print ('8 L - Liter')
convFROM = input('Unit: ')
if convFROM in acceptableInputs:
takeUnit2()
global convFROMconv
if convFROM == '1':
convFROMconv = 'Tablespoons'
elif convFROM == '2':
convFROMconv = 'Teaspoons'
elif convFROM == '3':
convFROMconv = 'Cups'
elif convFROM == '4':
convFROMconv = 'Quarts'
elif convFROM == '5':
convFROMconv = 'Fluid Ounces'
elif convFROM == '6':
convFROMconv = 'Gallons'
elif convFROM == '7':
convFROMconv = 'Milliliters'
elif convFROM == '8':
convFROMconv = 'Liters'
else:
print ('')
else:
print('That is not an acceptable input, please try again')
def takeUnit2 ():
os.system('cls')
print ('Select the unit you want to convert TO')
print ('1 Tbsp - Tablespoon')
print ('2 Tsp - Teaspoon')
print ('3 C - Cups')
print ('4 qt. - Quart')
print ('5 fl. oz. - Fluid Ounce')
print ('6 gal. - Gallon')
print ('7 ml - Milliliter')
print ('8 L - Liter')
convTO = input('Unit: ')
if convTO in acceptableInputs:
print('yay')
global convTOname #Making convTOconv global
global TOfunc1
if convTO == '1': #This whole statement converts the input number to its corresponding name
convTOname = 'Tablespoons'
TOfunc1 = 'TOtbsp'
TOtbsp()
elif convTO == '2':
convTOname = 'Teaspoons'
TOfunc1 = 'TOtsp'
elif convTO == '3':
convTOname = 'Cups'
TOfunc1 = 'TOcups'
elif convTO == '4':
convTOname = 'Quarts'
TOfunc1 = 'TOquarts'
elif convTO == '5':
convTOname = 'Fluid Ounces'
TOfunc1 = 'TOfloz'
elif convTO == '6':
convTOname = 'Gallons'
TOfunc1 = 'TOgal'
elif convTO == '7':
convTOname = 'Milliliters'
TOfunc1 = 'TOml'
elif convTO == '8':
convTOname = 'Liters'
TOfunc1 = 'TOliters'
else:
print ('')
else:
print('That is not an acceptable input, please try again')
takeUnit1()
def TOtbsp ():
os.system('cls')
print ('Convert tablespoons to: ' + convFROMconv)
print ('YAY')
感谢您的帮助!
答案 0 :(得分:0)
变量不是问题。
由于函数TOtbsp
而发生错误。
Python是脚本语言。 因此,您应该在使用之前定义一个函数。
将TOtbsp
移至源代码的顶部。