首页 > 解决方案 > 在哪里包含“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")

标签: pythonjsonapiloops

解决方案


推荐阅读