根据 python 中的用户输入更新多个变量之一
Updating one of several variables depending on user input in python
我正在 python 中编写我的第一个适当的项目,除了我书中的 CodeWars katas 和问题练习之外,它旨在计算锻炼计划中每个肌肉群的每周总训练量。
我写的是一本名为 bodypart
的大词典,其中 key
= 运动名称(即卧推)和 value
= 主要肌肉群(即胸部)。
程序然后要求用户使用以下代码输入练习和组数:
# offer option to see valid inputs, then get exercise from user
print('To see a list of possible exercises, enter "check".')
exercise = input('What is your first exercise of the day? ')
if exercise == 'check':
print(bodypart)
exercise = input('Please enter an exercise from the list. ')
while exercise not in bodypart:
exercise = input('Please enter an exercise from the list. ')
add_to_part = bodypart.get(exercise)
print('')
# get the number of sets and check for valid input
sets = input('How many sets will you do of this exercise? ')
if not sets.isdigit:
sets = input('Please enter a valid number.')
sets = int(sets)
我为每个主要 body 部分创建了一个计数变量,设置为 0。我接下来要做的似乎很长,我觉得必须有一个更优化的方法来这样做,但我我完全不知道该怎么做。我所做的是根据 bodypart
:
中的值添加相关计数器的集合数
# add sets to relevant counter
if add_to_part == 'biceps':
biceps += sets
if add_to_part == 'triceps':
triceps += sets
if add_to_part == 'chest':
chest += sets
if add_to_part == 'shoulders':
shoulders += sets
if add_to_part == 'back':
back += sets
if add_to_part == 'quads':
quads += sets
if add_to_part == 'hams':
hams += sets
if add_to_part == 'glutes':
glutes += sets
在 python 中有没有一种方法可以根据存储在 bodypart
中的字符串作为值来更新相关变量,而不是对每个个体使用 if
语句肌肉群?
您可以使用 dictionary 来实现您想要的行为
这是一个小代码片段 -
bodypart_sets = {
'biceps': 0,
'triceps': 0,
'chest': 0,
'shoulders': 0,
'back': 0,
'quads': 0,
'hams': 0,
'glutes': 0
}
print(list(bodypart_sets.keys()))
add_to_part = 'chest' # dynamic string
if add_to_part in bodypart_sets:
bodypart_sets[add_to_part] += 5
print(bodypart_sets['chest'])
print(bodypart_sets)
这会打印 -
['biceps', 'triceps', 'chest', 'shoulders', 'back', 'quads', 'hams', 'glutes']
5
{'biceps': 0, 'triceps': 0, 'chest': 5, 'shoulders': 0, 'back': 0, 'quads': 0, 'hams': 0, 'glutes': 0}
我正在 python 中编写我的第一个适当的项目,除了我书中的 CodeWars katas 和问题练习之外,它旨在计算锻炼计划中每个肌肉群的每周总训练量。
我写的是一本名为 bodypart
的大词典,其中 key
= 运动名称(即卧推)和 value
= 主要肌肉群(即胸部)。
程序然后要求用户使用以下代码输入练习和组数:
# offer option to see valid inputs, then get exercise from user
print('To see a list of possible exercises, enter "check".')
exercise = input('What is your first exercise of the day? ')
if exercise == 'check':
print(bodypart)
exercise = input('Please enter an exercise from the list. ')
while exercise not in bodypart:
exercise = input('Please enter an exercise from the list. ')
add_to_part = bodypart.get(exercise)
print('')
# get the number of sets and check for valid input
sets = input('How many sets will you do of this exercise? ')
if not sets.isdigit:
sets = input('Please enter a valid number.')
sets = int(sets)
我为每个主要 body 部分创建了一个计数变量,设置为 0。我接下来要做的似乎很长,我觉得必须有一个更优化的方法来这样做,但我我完全不知道该怎么做。我所做的是根据 bodypart
:
# add sets to relevant counter
if add_to_part == 'biceps':
biceps += sets
if add_to_part == 'triceps':
triceps += sets
if add_to_part == 'chest':
chest += sets
if add_to_part == 'shoulders':
shoulders += sets
if add_to_part == 'back':
back += sets
if add_to_part == 'quads':
quads += sets
if add_to_part == 'hams':
hams += sets
if add_to_part == 'glutes':
glutes += sets
在 python 中有没有一种方法可以根据存储在 bodypart
中的字符串作为值来更新相关变量,而不是对每个个体使用 if
语句肌肉群?
您可以使用 dictionary 来实现您想要的行为
这是一个小代码片段 -
bodypart_sets = {
'biceps': 0,
'triceps': 0,
'chest': 0,
'shoulders': 0,
'back': 0,
'quads': 0,
'hams': 0,
'glutes': 0
}
print(list(bodypart_sets.keys()))
add_to_part = 'chest' # dynamic string
if add_to_part in bodypart_sets:
bodypart_sets[add_to_part] += 5
print(bodypart_sets['chest'])
print(bodypart_sets)
这会打印 -
['biceps', 'triceps', 'chest', 'shoulders', 'back', 'quads', 'hams', 'glutes']
5
{'biceps': 0, 'triceps': 0, 'chest': 5, 'shoulders': 0, 'back': 0, 'quads': 0, 'hams': 0, 'glutes': 0}