我如何在Python上循环/重复程序?

问题描述 投票:1回答:2

我已经尝试了一点,但是没有办法去做/理解它。我想重复执行该程序,并且仅在用户手动关闭程序时才关闭。这是代码:

from json import dumps
from lcu_driver import Connector

connector = Connector()

async def set_icon():

# number of icon
iconID = input("Put a number here")
# make the request to set the icon
icon = await connector.request('put', '/lol-summoner/v1/current- summoner/icon',
                           data=dumps({'profileIconId': iconID}))

# if HTTP status code is 201 the icon was applied successfully
if icon.status == 201:
print(f'icon number {iconID} was set correctly.')
else:
print('Unknown problem, the icon was not set.')

# fired when LCU API is ready to be used
@connector.event
async def connect():
print('LCU API is ready to be used.')

# check if the user is already logged into his account
summoner = await connector.request('get', '/lol-summoner/v1/current-    summoner')
if summoner.status == 200:
    data = await summoner.json()

    # calls login method and update login.left_calls to 0
    # when login.left_calls is 0 the function can't be fired any more     neither by websocket nor manually
    await login(None, None, data)

else:
    print('Please login into your account to change your icon...')


# fired when League Client is closed (or disconnected from websocket)
@connector.event
async def disconnect():
print('The client have been closed!')

# subscribe to the login websocket event, and calls the function only one     time
@connector.ws_events(['/lol-summoner/v1/current-summoner'], event_types=    ['Update'],
                 max_calls=1)
async def login(typ, uri, data):
print('Logged as', data['displayName'])
await set_icon()


# opens websocket connection (may be used to wait until the client is             closed)
connector.listen()
# starts everything
connector.start()

进行此重复的最佳方法是什么?我希望即时询问是有意义的,英语不是我的母语。

谢谢。

python python-3.x loops
2个回答
0
投票

您可以尝试以下方法:

while True:
  # number of icon
  iconID = input("Put a number here (exit to exit): ")
  if iconID == 'exit':
     break
  # make the request to set the icon
  icon = await connector.request('put', '/lol-summoner/v1/current-summoner/icon',
                               data=dumps({'profileIconId': iconID}))

  # if HTTP status code is 201 the icon was applied successfully
  if icon.status == 201:
     print(f'icon number {iconID} was set correctly.')
  else:
     print('Unknown problem, the icon was not set.')

0
投票

无限期重复的最简单方法是将其包装在while循环中。假设您正在终端中运行此程序,则可以使用CTRL + C或CTRL + Z停止它。

while True:
    # number of icon
    iconID = input("Put a number here: ")

    # make the request to set the icon
    icon = await connector.request('put', '/lol-summoner/v1/current-summoner/icon',
                                   data=dumps({'profileIconId': iconID}))

    # if HTTP status code is 201 the icon was applied successfully
    if icon.status == 201:
        print(f'icon number {iconID} was set correctly.')
    else:
        print('Unknown problem, the icon was not set.')

您说自己无法自己解决这个事实,这使我想知道您是否真正在寻找更具体/非常规的内容,在这种情况下,您需要对问题进行更具体的说明。无论如何,我将其作为答案以防万一。

© www.soinside.com 2019 - 2024. All rights reserved.