2

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)
1
  • I dont know what you are exactly looking for. But based on dynamic new_url_input. You can use inline lambda method first line inside forloop. something like this... new_url = get_new_url_input() Commented Mar 27, 2019 at 21:52

1 Answer 1

1

When you call input() it returns a string and in the loop you're just assigning that string to a new variable. You'll need to call input() again in some way, even if it's wrapping it in a function, for example using lambda as below...

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()

EDIT: Now that I understand what you're saying (the instructions on the input prompt helped), I think this is what you're trying to do...

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...
Sign up to request clarification or add additional context in comments.

5 Comments

This wont work for me because I want to take the input first so even when doing this placing the new_url_input somewhere doesn't run until it gets to the For loop I want to ask the question get the input and use it later in/for the For loop .
@Unknown Check out my edit for something I think will help.
Thanks but when I run it I get this error new_url = new_url_inputs[number] IndexError: list index out of range
Because you must have more screen shots than entered URLs, so catch the exception and deal with it however you want.
Sorry I don't understand how to deal with this yet still very new to Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.