python - 在哪里包含“while 循环”以多次要求用户输入?Python
问题描述
我正在开发一个简单的应用程序,它将为用户搜索活动。我遇到了两个问题。如果用户不满意,我尝试包含“while 循环”来搜索新活动。
示例 JSON
{
"activity":"LearnGraphQL",
"type":"education",
"participants":1,
"price":0,
"link":"https://graphql.org/",
"key":"2167064",
"accessibility":0.8
}
你能推荐如何解决这个问题吗?
另一件不起作用的事情是在这部分:
for item in activity_possibilities :
if question2 == 'y':
a = Activity(activity_list, activity_possibilities["participants"] > 1)
如何使第二个参数返回“参与者”键中的值高于 1 的活动?
import requests
import json
class Activity :
def __init__(self, activity, participants) :
self.activity_activity = activity
self.activity_participants = participants
activity_type = ("education", "recreational", "social", "diy",
"charity", "cooking", "relaxation", "music", "busywork")
url = "http://www.boredapi.com/api/activity?type="
question1 = input(f"Select one of the folowing: {activity_type}")
question2 = input(f"\nDo you have company? y/n")
response = requests.get(url + question1)
activity_possibilities = response.json()
activity_list = activity_possibilities["activity"]
activity_objects = []
for item in activity_possibilities :
if question2 == 'y':
a = Activity(activity_list, activity_possibilities["participants"] > 1)
else:
a = Activity(activity_list, activity_possibilities["participants"] == 1)
activity_objects.append(a)
print(f"\nMaybe you can try {a.activity_activity}")
last_question = input("\nDo you like this activity? y/n")
解决方案
推荐阅读
- webpack - 如何从数组(webpack)中要求每个循环中的图像
- java - 如何在 HIbernate 中聚合函数
- java - 内部类的属性具有外部类的泛型类型
- javafx - JavaFx 大小布局
- angularjs - Angular.js 从 textarea 字段文本中删除换行符
- node.js - 压缩 Node 应用程序并让依赖项开箱即用
- sql - 从反斜杠 SQL/SSIS 中分隔字符串
- xml - 为每个节点找到一个 XML 但不删除?
- javascript - 数组中的过滤元素在 Reactjs 中不起作用
- emacs - 通过 org-sbe 将 org-mode 属性传递给源代码块