Python 3.x 我想用变量列表自动化python函数
上面的代码就是我所拥有的。我想要它,这样我就有了用户输入的变量,我是否可以通过for循环或其他方式自动触发函数。下面是一个我认为您希望实现的简化示例。我们指定所需成分的列表,并要求用户输入项目,直到他们拥有所有这些成分。我在比较期间使用,因为我们不关心项目的输入顺序Python 3.x 我想用变量列表自动化python函数,python-3.x,Python 3.x,上面的代码就是我所拥有的。我想要它,这样我就有了用户输入的变量,我是否可以通过for循环或其他方式自动触发函数。下面是一个我认为您希望实现的简化示例。我们指定所需成分的列表,并要求用户输入项目,直到他们拥有所有这些成分。我在比较期间使用,因为我们不关心项目的输入顺序 import sys print('Do you want a cup of tea?') user_input_tea = input().lower() if user_input_tea == 'yes': pr
import sys
print('Do you want a cup of tea?')
user_input_tea = input().lower()
if user_input_tea == 'yes':
print("You feel the thirst for a nice cup of tea, now it's time to make it.")
else:
print('You feel a cup of tea is not for you.')
sys.exit(0)
equipment = []
def equipment_find(item):
if item == str(item):
print("You have acquired the " + str(item) + " , now it's time to proceed.")
equipment.append(item)
print(equipment)
else:
print("You did not enter: " + str(item))
while True:
print("Please input: " str(item))
if item == str(item):
equipment.append(item)
print(equipment)
break
print("You are going to need a kettle for this tea making process, please input kettle to acquire the kettle")
kettle = input().lower()
equipment_find(kettle)
你的最终目标是什么?你想让测试更容易,这样就不必手动输入?如果您只是想避免用户输入,只需将输入语句替换为实际字符串即可。例如,
user\u input\u tea='yes'
等等。我想让用户输入,并创建更多变量,我只想让它也循环,这样我就不必每次都键入设备查找(变量),并让它循环所有给定变量,直到所有变量都被覆盖,然后让代码继续运行,它将进入另一个功能,我稍后会写进去。我想添加一个包含所有变量的列表或字典,然后使用for循环遍历列表并为列表中的每个变量触发函数,但我不知道如何执行此操作。我是一个新的编码btwDictionary将工作得很好,如果你想支持更多,然后只是茶。所以你可以做一些事情:<代码> CydiPixtt='Toe':[水壶','水','勺子','糖'),'谷类':[牛奶','薄片','碗' } /代码>如果你正在寻找正确地学习Python的基础,请查看本教程,它真的非常好:
requiredItemsList = ['kettle', 'water', 'spoon', 'sugar']
enteredItmesList = []
print("You feel the thirst for a nice cup of tea, now it's time to make it.")
while set(requiredItemsList) != set(enteredItmesList):
item = input('Enter an item: ')
if item in requiredItemsList and item not in enteredItmesList:
print('You have acquired the', item, ", now it's time to proceed.")
enteredItmesList.append(item)
else:
print('The item you entered is not valid. Please try again.')
print('You have aquired all needed items!')
print(enteredItmesList)