import calories
height = calories.get_non_negative_int("Please enter your height: ")
weight = calories.get_non_negative_int("Please enter your weight: ")
age = calories.get_non_negative_int("Please enter your age: ")
print()
again = 'y'
while again == 'y':
print('What would you like to do ?')
print('To find out the calories you need per day to sustain your weight press 1.')
print('To enter a calorie amount and calculate how many minutes of RUNNING required to burn those calories press 2.')
print('To enter a calorie amount and calculate how many minutes of JOGGING required to burn those calories press 3.')
print('To enter a calorie amount and calculate how many minutes of WALKING required to burn those calories press 4.\n')
selection = calories.get_selection('Please enter the number of your selection (1-4): ')
if selection == 1:
print('You need to burn', calories.standardCalBurn(height, weight, age), 'calories per day to sustain your current weight.\n')
again = input('Would you like to calculate more? (y/n): ')
if selection == 2:
weight = global weight
caloriesToBurnOff = int(input('Please enter the amount of calories you would like to calculate: '))
calsBurnedPerMinRunning = calories.burnedRunning(weight)
print(calories.timeRequiredRun(caloriesToBurnOff, calsBurnedPerMinRunning))
在我的代码中,在第二行到最后一行的位置显示calsBurnedPerMinRunning = calories.burnedRunning(weight)
,python告诉我weight
未定义,当我已经向用户询问变量时。我尝试在它面前添加global
并且没有运气。我现在很难过,希望得到任何帮助。
编辑:-----
import calories
height = calories.get_non_negative_int("Please enter your height: ")
weight = calories.get_non_negative_int("Please enter your weight: ")
age = calories.get_non_negative_int("Please enter your age: ")
print()
again = 'y'
while again == 'y':
print('What would you like to do ?')
print('To find out the calories you need per day to sustain your weight press 1.')
print('To enter a calorie amount and calculate how many minutes of RUNNING required to burn those calories press 2.')
print('To enter a calorie amount and calculate how many minutes of JOGGING required to burn those calories press 3.')
print('To enter a calorie amount and calculate how many minutes of WALKING required to burn those calories press 4.\n')
selection = calories.get_selection('Please enter the number of your selection (1-4): ')
if selection == 1:
print('You need to burn', calories.standardCalBurn(height, weight, age), 'calories per day to sustain your current weight.\n')
again = input('Would you like to calculate more? (y/n): ')
if selection == 2:
caloriesToBurnOff = int(input('Please enter the amount of calories you would like to calculate: '))
calsBurnedPerMinRunning = calories.burnedRunning(weight)
print(calories.timeRequiredRun(caloriesToBurnOff, calsBurnedPerMinRunning))
##calories.py -- function definitions ##
## Michael Melendez
## March 3, 2018
def standardCalBurn(height, weight, age):
calsNeededPerDay = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age)
return calsNeededPerDay
def burnedRunning(weight):
calsBurnedPerMinRunning = weight * 0.095
return calsBurnedPerMinRunning
def burnedJogging(weight):
calsBurnedPerMinJogging = weight * 0.0775
return calsBurnedPerMinJogging
def burnedWalking(weight):
calsBurnedPerMinWalking = weight * 0.054
return calsBurnedPerMinWalking
def timeRequiredRun(caloriesToBurnOff, calsBurnedPerMinRunning):
calsBurnedPerMinRunning = burnedRunning(weight)
time = caloriesToBurnOff / calsBurnedPerMinRunning
#print(time)
return timeRequiredRun
def timeRequiredJog(caloriesToBurnOff, calsBurnedPerMinJogging):
calsBurnedPerMinJogging = burnedJogging(weight)
time = caloriesToBurnOff / calsBurnedPerMinJogging
#print(time)
return timeRequiredJog
def timeRequiredWalk(caloriesToBurnOff, calsBurnedPerMinWalking):
calsBurnedPerMinWalking = burnedWalking(weight)
time = caloriesToBurnOff / calsBurnedPerMinWalking
#print(time)
return timeRequiredWalk
#-------------------------------------#
def get_non_negative_int(prompt):
while True:
try:
value = int(input(prompt))
except ValueError:
print("Error, please enter a valid value.")
continue
if value < 1:
print("Error, please enter a valid number.")
continue
else:
break
return value
def get_selection(prompt):
while True:
try:
value = int(input(prompt))
except ValueError:
print("Error, please enter a valid value.")
continue
if value < 1 or value > 4:
print("Error, please make a valid selection.")
continue
else:
break
return value
以下是我收到的追溯错误:
Traceback (most recent call last):
File "/Users/michaelmelendez/Desktop/Vicky's Gym/Vicky.py", line 23, in <module>
print(calories.timeRequiredRun(caloriesToBurnOff, calsBurnedPerMinRunning))
File "/Users/michaelmelendez/Desktop/Vicky's Gym/calories.py", line 22, in timeRequiredRun
calsBurnedPerMinRunning = burnedRunning(weight)
NameError: name 'weight' is not defined
答案 0 :(得分:1)
将weight = global weight
替换为global weight
答案 1 :(得分:1)
您甚至不需要global
,因为您的脚本中没有任何内容引入范围。正确的解决方法不是将weight = global weight
替换为global weight
,而是完全删除该行。
- 编辑 -
请仔细查看错误消息。
Traceback (most recent call last): File "main.py", line 26, in <module> print(calories.timeRequiredRun(caloriesToBurnOff, calsBurnedPerMinRunning)) File "/path/to/calories.py", line 22, in timeRequiredRun calsBurnedPerMinRunning = burnedRunning(weight) NameError: global name 'weight' is not defined
检查calories.py的第22行。
答案 2 :(得分:0)
你不能在同一行中定义和分配全局变量
试试这个
global weight
weight = 5