Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中为For语句内部的输入字段循环一个变量_Python_Json_Python 3.x - Fatal编程技术网

在Python中为For语句内部的输入字段循环一个变量

在Python中为For语句内部的输入字段循环一个变量,python,json,python-3.x,Python,Json,Python 3.x,我想向用户显示另一个输入字段,但除非我在For循环中为new\u urlnew\u url=input(“请为屏幕截图输入新url(按回车键停止):”),否则它无法工作但我想将输入字段移动到For循环之外的某个地方,所以我尝试对输入字段执行此操作,如new\u url=new\u url\u input并获取new\u url\u input变量,然后将其添加到代码中的其他地方,如new\u url\u input=input(“请为屏幕截图输入新url(按回车键停止):”).strip()。有

我想向用户显示另一个输入字段,但除非我在For循环中为
new\u url
new\u url=input(“请为屏幕截图输入新url(按回车键停止):”),否则它无法工作
但我想将输入字段移动到For循环之外的某个地方,所以我尝试对输入字段执行此操作,如
new\u url=new\u url\u input
并获取
new\u url\u input
变量,然后将其添加到代码中的其他地方,如
new\u url\u input=input(“请为屏幕截图输入新url(按回车键停止):”).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...

我不知道你到底在找什么。但基于动态的新url输入。您可以在forloop的第一行使用内联lambda方法。像这样的。。。new\u url=get\u new\u url\u input()这对我来说不起作用,因为我想先获取输入,所以即使在执行此操作时,放置
新的\u url\u输入
直到到达for循环时才会运行。我想问一个问题:获取输入,然后在for循环中使用它。@Unknown查看我的编辑,我认为会有所帮助。谢谢,但当我运行它时,我得到了这个问题错误
new\u url=new\u url\u inputs[number]indexer错误:列表索引超出范围
,因为您必须有比输入的url更多的屏幕截图,所以捕获异常并根据需要进行处理。很抱歉,我不明白如何处理此问题,但对Python来说还是很陌生的。