How to Get Max Output from a While Loop

How to get Max output from a while loop

You should add variable (max_amount) which will store value of money_in_pot and update it when roll is 7 and current amount more then initial:

import random

count = 0
number = 0
money_in_pot = float(input('Enter amount of money you want to put in pot: '))
max_amount = money_in_pot # store initial value
while money_in_pot > 0:
dice_roll = random.randint(1, 7), random.randint(1, 7)
print(dice_roll)
roll = sum(dice_roll)
count += 1
if roll == 7:
money_in_pot += 4
if money_in_pot > max_amount: # update max if it more then before
max_amount = money_in_pot
print ('You rolled a 7.')
else:
money_in_pot -= 1
print ('You did not roll a 7.')
print ('You are out of money in {} turn/s. Max amount was: {}.'.format(count, max_amount))

Unable to get while loop working to get min and max of numbers

You're comparing strings, so it's doing a lexicographical order, where 6 is larger than 10. You need to convert the number strings to actual numbers before comparing:

str_num = input("Enter a number: ")
if str_num == "done":
break
num = int(str_num) # Convert to integer

php max value of array in while loop

Many thanks for the ideas here, I got this solved using the following. It may be more convoluted than the answer from @Qirel but there were extra complications I didn't mention as I didn't want to create a mess. For instance these tables are from 2 separate databases which use specific connections and I had to rely on variables as the data in the first table is user created and retrieved based on the value of a cookie.

Thanks to @rpm192, it was almost there.

<?php
$select_table1 = "select * from table1";
$connect_table1 = mysqli_query($con, $select_table1);
while ($row_table1 = mysqli_fetch_array($connect_table1)) {
$type = $row_table1['type'];
$category = $row_table1['category'];

$select_table2 = "select * from table2 where type in('$type') and category in('$category')";
$connect_table2 = mysqli_query($con, $select_table2);
while ($row_table2 = mysqli_fetch_array($connect_table2)) {
$amount = $row_table2['amount'];
$myArray[] = $amount;
}
}
$maxAmount = max($myArray);
echo $maxAmount;
?>

Python Min Max While loop

The output for "code 1" is different from what you expect because the operands in the comparison are str, for which the defined order is different from the order of the corresponding int.

In other words, if a and b are two ints such that a > b, then we can be sure that str(a) > str(b) only if the length of the two string is the same, otherwise it can happen either str(a) > str(b) or str(a) < str(b) depending on the actual value of a and b.

The ordering of string consisting of numbers is "alphabetical" on characters appearing first, hence, while "1" < "2" < "3", etc., when the two strings have multiple characters, either option is open e.g.:

  • for "2" and "10", "2" > "10" (opposite of 2 < 10) because "2" > "1" and the presence of "0" after the "1" is irrelevant
  • for "2" and "20", "2" < "20" (same as 2 < 20) because "2" == "2" and the presence of "0" after the "2" in "20" makes "20" "bigger".

Note that the string ordering/comparison actually work for any string, even those not containing numbers.

How to find max&min in while loop - c

Printing an int using a conversion specifier for double is not a good idea, but invokes the infamous Undefined Behaviour.

The compiler might have noticed you about this. If it didn't, then increase its warning level. For GCC use -Wall -Wextra -pedantic.

So to fix this either make min and max be double

  int a = 0, b = 0;
double min = 1000., max = 0.; /* Such initialisations limit your input. */

or leave them be int (decreasing accuracy), and print them as what there are, namely int

  printf("Minimum: %d\nMaximum: %d\n", min, max);

while loop for largest and smallest values Error

removing the else will solve the problem:

int main()
{
int num, minimum=10000000 , max=0;
int choice;
printf(" To Exit Enter (-1)\n");

while(num!=-1)
{

printf(" enter a number: ");
scanf("%d",&num);

if(num>max)
{
max=num;
}

if(num<minimum && num!=-1)
{
minimum=num;
}
}


printf("\n Largest Number: %d \n Smallest number: %d",max,minimum);
}

the problem was that the if condition was always true because you were entering ascending numbers so the else condition was never executed



Related Topics



Leave a reply



Submit