This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Devaka Cooray
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# finding the highest value in variables using a loop

Ranch Hand
Posts: 47
if i have

numberOne = 34
numberTwo = 23
numberThree = 17
numberFour = 8

how can i find the highest value using a for loop in python?

i know you can use max and that is very cool

but i have to solve it using a loop

i cant get the loop iterating over all 4 values to determine which is the highest...

Marshal
Posts: 62881
203

warren brandt wrote:. . . highest value using a for loop in python? . . .

I know one way to get the maximum; you can get the minimum simultaneously, but the technique is the same in all languages. It is actually the same as you would use with pencil and paper. So let's have some numbers:

int[] randomNumbers = new Random().ints(100L, 1000, 2000).toArray();
randomNumbers ==> int[100] { 1150, 1808, 1889, 1894, 1432, 1785, 16 ... , 1359, 1773, 1917, 1237 }

jshell> randomNumbers
randomNumbers ==> int[100] { 1150, 1808, 1889, 1894, 1432, 1785, 1610, 1677, 1471, 1886, 1644, 1400, 1590, 1018, 1737, 1207, 1283, 1158, 1904, 1324, 1609, 1373, 1537, 1908, 1147, 1196, 1498, 1470, 1775, 1743, 1676, 1233, 1517, 1103, 1965, 1795, 1115, 1271, 1490, 1275, 1871, 1310, 1361, 1056, 1071, 1439, 1761, 1522, 1400, 1187, 1166, 1686, 1297, 1753, 1085, 1080, 1411, 1475, 1544, 1777, 1071, 1754, 1526, 1519, 1996, 1860, 1282, 1980, 1660, 1193, 1806, 1246, 1754, 1334, 1029, 1661, 1571, 1248, 1322, 1900, 1678, 1290, 1504, 1471, 1944, 1618, 1830, 1091, 1304, 1525, 1638, 1195, 1948, 1979, 1323, 1446, 1359, 1773, 1917, 1237 }

That program will produce a 100‑element array with numbers chosen between 1,000 and 2,000. Please read the details here otherwise you might get confused.
Now, using the first element (=1150) as a first guess, work out the algorithm as you go through the array. It is not difficult.
What do you mean by not being able to get the loop to iterate all four values? Do you mean you can't get the loop to run, or something different? You would want to get your four values into some sort of data structure, otherwise you will find the procedure too difficult.

warren brandt
Ranch Hand
Posts: 47
hi campbell not allowed to use arrays either

Campbell Ritchie
Marshal
Posts: 62881
203
Are you really supposed to write code to test value1, value2, value3, and value4 separately? The idea of the array was so you can see the logic of going through an array looking for the largest element.

 See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad: Programmatically Create PDF Using Free Spire.PDF with Java https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire