'Can anyone explain that how to print max and min in the code
This is a code
random is used in the code
the query is to find max and min value of z
import random x = random.randint(2,6) y = random.randint(1,2) z = x + y print(z)
Solution 1:[1]
Okay not a lot to go with but I think this is what you're trying to get:
import random
x = random.randint(2, 6)
y = random.randint(1, 2)
z = max(x, y)
print(z)
This would return the max value between x and y into the variable z
Solution 2:[2]
If you want max and min values of z then you are going to need at least two values for z, perhaps more. I suspect there is a part of the question that will say how many.
Here is some pseudocode to help you:
// Set extreme initial values for max, min.
maxZ <- -1
minZ <- 1000
// Try a few values of z.
repeat
// Generate one z.
x <- random(2, 6)
y <- random (1, 2)
z <- x + y
// Check for new max, min.
if (z > maxZ) maxZ <- z
if (z < minZ) minZ <- z
until (enough z's checked)
// Display results.
print "Max z = " maxZ
print "Min z = " minZ
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Xanik |
| Solution 2 | rossum |
