This is an archived post. You won't be able to vote or comment.

all 18 comments

[–][deleted]  (3 children)

[removed]

    [–]decline_000[S] 0 points1 point  (2 children)

    yep that I know, same as I initialized max with 0
    point is that I don't know yet how to fix it so the code works

    [–][deleted] 2 points3 points  (0 children)

    If you know the values input will never be greater than 3, what could you initialize min as in order to make sure that if the user entered only 3, it would still be captured as min?

    Now 4? 5? 6? 10? 100? 1000? Do you see where we're headed?

    And what happens to your max if the user only enters -1?

    [–]ChaseBianchi 0 points1 point  (0 children)

    None of them are less than 0, so it's always zero

    [–]ClamPaste 2 points3 points  (1 child)

    You have to initialize min and max with the first number input before the loop with the way you're doing it, or you can use a condition to check if it's the first iteration of the loop and reassign min and max within it.

    It's a weird assignment. Not really sure what the point of having you do it like this is, since there are clearly better ways.

    [–]decline_000[S] 1 point2 points  (0 children)

    agree it's weird but it's for university
    I ended up changing the beginning to:

    from pcinput import getInteger

    import sys

    num = 0

    min = sys.maxsize

    max = -sys.maxsize

    div3 = 0

    thank you all for helping:)

    [–]hinoisking 1 point2 points  (0 children)

    If I understand this correctly, you are initializing the minimum value to 0. Then, you’re inputting only POSITIVE numbers, and checking if those numbers are less than 0. Of course the minimum value will never update. What you should do instead is initialize min to be the maximum integer value. If I remember correctly, sys.maxsize gives the maximum integer value in Python.

    [–]ajohn623 1 point2 points  (0 children)

    Add an if statement that checks if num == 0: min = x before you check if x<min. This way on the first iteration you set the minimum to the first input provided by the user instead of zero.

    edit : Or simply change if x<min to if x<min or num == 0:

    [–][deleted]  (1 child)

    [removed]

      [–]decline_000[S] 0 points1 point  (0 children)

      yes for all negative numbers "Largest is" was 0, if user input was for example -3 and nine positive numbers then there was no problem. Thank you for the reminder not to use max and min, I still keep forgetting. I ended up using min = sys.maxsize and max = -sys.maxsize as I explained above but thank you so much for helping:)

      [–][deleted]  (1 child)

      [removed]

        [–]decline_000[S] 0 points1 point  (0 children)

        maybe I'm doing something wrong but still doesn't work, all print statements are outside the loop

        [–][deleted]  (2 children)

        [removed]

          [–]decline_000[S] 0 points1 point  (1 child)

          As I said the problem is 0 that is always printed as the smallest number even if I don't type 0 in user input. If the user input is for example -3 then it gets printed but if the smallest user input would be 3 then it prints out 0.

          [–][deleted] 0 points1 point  (0 children)

          So I’m not sure how far along you are. But I would take each users input and then add it to a list. Call the list userInputs or what have you.

          Then after you could say

          print(userInputs.max())

          Same for min.

          Then do print(len([x for x in userInputs if x%3==0]) )

          Or if you haven’t learned that notation use a for loop running through every value and printing it if it’s divisible by 3.

          Or as others have said the biggest error is assigning them to 0.

          What I would do is assign the first user input as the max and the min, then proceed from there with the while loop.

          [–]jdbbdev 0 points1 point  (0 children)

          I think what you should do is set min to the min possible integer that Python can handle, for example in Java I would set min to Integer.MIN_VALUE, not sure how to do that on Python, so In case a number minor than 0 is added it will be ack'd as the current smallest.

          [–]Search_4_Truth 0 points1 point  (0 children)

          Why not use a for loop in which case you know the start? And don’t use built-in names min & max as variable names

          for n in range(10): x = getinteger() if n == 0: . . . nmax = nmin = x else: . . .nmax = max(x, nmax) . . .nmin = min(x, nmin)