I want to show the users another input field but it doesn't work unless I do this inside the For loop for new_url
new_url = input("Please enter new URL for a screenshot (press return to stop): ").strip()
but I want to move the input field to somewhere outside of the For loop so I tried doing this for the input field like new_url = new_url_input
and take the new_url_input
varable and add it somewhere else in my code like new_url_input = input("Please enter new URL for a screenshot (press return to stop): ").strip()
but when I do this it only displays the code once but it should work like when the user presses enter it show another input field. Please see this question/answer for more information about my topic.
Original Code:
# 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)
Example of how I want it to work/look:
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)