나는 사용자에게 다른 입력 필드를 보여주고 싶어하지만 난에 대한 루프를 위해 내부에이 작업을 수행하지 않는 한이 작동하지 않습니다 new_url
new_url = input("Please enter new URL for a screenshot (press return to stop): ").strip()
하지만 난 같은 입력 필드에이 일을 시도 있도록 루프를 들어 어딘가의 외부 입력 필드를 이동할 new_url = new_url_input
및 new_url_input
변수를 가져 와서 내 코드의 다른 곳에 추가하십시오. new_url_input = input("Please enter new URL for a screenshot (press return to stop): ").strip()
하지만 이렇게하면 코드가 한 번만 표시되지만 사용자가 입력을 누르면 다른 입력 필드가 표시되는 것처럼 작동합니다. 내 주제에 대한 자세한 내용 은이 질문 / 답변 을 참조하십시오.
원래 코드 :
# Load the data
file_name = file_name = path/to/json/file
with open(file_name) as fh:
full_data = json.load(fh)
# Dig into the data to find the screenshots
screen_shots = full_data['tabs'][0]['views'][1]['screenshots']
# Loop over each screen shot, updating each one
for number, screen_shot in enumerate(screen_shots):
new_url = input("Please enter new URL (press return to stop): ").strip()
if new_url:
screen_shot.update({"url": new_url, "fullSizeURL": new_url})
else:
break
# Remove all entries which we did not update
screen_shots = screen_shots[:number]
# Save the data
with open(file_name, 'w') as fh:
json.dump(full_data, fh, indent=4)
내가 원하는 작동 / 모양의 예 :
new_url_input = input("Please enter new URL (press return to stop): ").strip()
# Load the data
file_name = path/to/json/file
with open(file_name) as fh:
full_data = json.load(fh)
# Dig into the data to find the screenshots
screen_shots = full_data['tabs'][0]['views'][1]['screenshots']
# Loop over each screen shot, updating each one
for number, screen_shot in enumerate(screen_shots):
new_url = new_url_input
if new_url:
screen_shot.update({"url": new_url, "fullSizeURL": new_url})
else:
break
# Remove all entries which we did not update
screen_shots = screen_shots[:number]
# Save the data
with open(file_name, 'w') as fh:
json.dump(full_data, fh, indent=4)
호출 input()
하면 문자열이 반환되고 루프에서 해당 문자열을 새 변수에 할당하는 것입니다. input()
예를 lambda
들어 아래와 같이 사용하여 함수로 래핑하더라도 어떤 식 으로든 다시 호출해야 합니다.
new_url_input = lambda: input("Please enter new URL (press return to stop): ").strip()
# ...other code...
for number, screen_shot in enumerate(screen_shots):
new_url = new_url_input()
편집 : 이제 당신이 말하는 것을 이해 했으므로 (입력 프롬프트의 지침이 도움이되었습니다), 이것이 당신이하려는 일이라고 생각합니다 ...
new_url_inputs = []
input_prompt = 'Please enter new URL (press return to stop): '
new_url_input = input(input_prompt).strip()
while new_url_input:
new_url_inputs.append(new_url_input)
new_url_input = input(input_prompt).strip()
# ...other code...
for number, screen_short in enumerate(screen_shots):
new_url = new_url_inputs[number]
# ...etc...
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다