How to Make a Function Change Variables While in a While Loop

How to change variable in a while loop from hardcoded to incorporate pandas iterrows loop?

Just add the for loop to iterate through your list and use that variable in the url.

A few other things I'd clean up here:

  1. I would use f'{}' syntax for the url, but how you had it is fine...just preference, as I think it's easier to read
  2. No need to use json package to read in the response. You can do that straight away (see edit below)

I'm also making an assumption here that you are setting an initial value for both variables start_index and max_results as this code will throw an error of those variables not being defined once it enters the while loop.

Code:

import pandas as pd

toget = {'id': [3396750, 3396753, 3396755, 3396757, 3396759]}

for eachId in toget['id']:
while result_count != 0:
start_at = "startAt=" + str(start_index)
url = url = f'{base_url}{eachId}&{start_at}&{max_results}'
response = requests.get(url, auth=(username, password))
json_response = json.loads(response.text)
print (json_response)
page_info = json_response["meta"]["pageInfo"]
start_index = page_info["startIndex"] + allowed_results
result_count = page_info["resultCount"]
items2 = json_response["data"]
print(items2)

how to change variable value automatically in while loop?

Your code is changing f from 1 to 0 and then immediately changing it back again, so your code is working, but it switches the light off so quickly again you don't see it.

Change this line

if f == '0':

to

elif f == '0':

While loop change variables

Put the values in an array and access the values:

a = ['A', 'B', 'C', 'D', 'E']

i = 0
while i<5:
print(a[i])
i += 1

Changing variable within while loop

There's a few ways to do it...

$boss = new array(1, 2, 3, 4);
foreach($boss as $value){
switch($value){
case 1:
//do something
break;
case 2:
//something else;
break;
}
}

Would probably be how I wrote it.

Edit: If you're looking for clean code, you can do something like this:

bosshandler.php:

function doSomethingWithBoss($value){
switch($value){
case 1:
return "boss equals 1";
break;
case 2:
return "boss equals 2";
break;
}
}

Your script page:

include bosshandler.php;
$boss = new array(1, 2, 3, 4);
foreach($boss as $value){
echo doSomethingWithBoss($value);
}

Ok, trying again. I think I see what you're asking now:

You want to assign a variable so that your arrays match up. If you're in $boss[1] you want to assign something to $boss_holder['1'], is that correct? If so, you don't want to use a foreach array, as it can be somewhat unpredictable with arrays and the order in which it parses them.

Instead, let's use a for() loop, like so:

$boss = new Array(1, 2, 3, 4);
$boss_holder = new Array(); //empty
$length = count($boss);
//iterate through boss
for($i = 1; $i < $length; $i++){
if($boss[$i] == 1){
$boss_holder[$i] = $tick;
}elseif($boss[$i] == 2){
$boss_holder[$i] = $cross;
}
}

Edit again to reflect your full code:

Use arrays. Refactor your code so that it uses arrays. If for some reason you absolutely cannot or will not use arrays, you can use PHP's variable variables to accomplish what you're looking for. It would look something like this:

var $boss1_holder, $boss2_holder, $boss3_holder;
for($i=1; $i> 4; $i++){
$variableName = 'boss' . $i . '_holder';
$$variableName = 1;
}
echo $boss2_holder; //returns 2

Variable outside or inside while loop?

When you set the value of a variable with mid = (low + high) // 2, the value is calculated using the current values of other variables. This is a one-and-done operation. mid will not automatically update if the value of low or high changes. So you need this assignment in the while loop to update mid.

While loop modify global variable

You have declared i as a global variable and then you are using it in the loop.
So, whatever changes you make to i in your program will be applied on that i since it's a global variable.

Global variable means i has a global scope. If you had created i inside a function, it would have a local scope and could not be accessed outside of the function.

So in your case, changes will be made and output will be 10.



Related Topics



Leave a reply



Submit