首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想用一个变量列表自动化一个python函数。

问我想用一个变量列表自动化一个python函数。
EN

Stack Overflow用户
提问于 2016-03-10 22:22:57
回答 1查看 189关注 0票数 0
代码语言:javascript
复制
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)

上面的代码就是我所拥有的。我想要它,所以我已经有了用户输入的变量,有没有什么我可以用for循环或其他东西自动触发的函数。

EN

回答 1

Stack Overflow用户

发布于 2016-03-11 00:04:07

这里有一个我认为你想要实现的简化示例。我们指定了所需配料的列表,并要求用户输入项目,直到他们拥有所有项目。我在比较过程中使用set(),因为我们不关心条目输入的顺序。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35919086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档