Home > Net >  Define variable in for loop with an unknown number or user number - Noob CS Student
Define variable in for loop with an unknown number or user number - Noob CS Student

Time:04-08

I am a noob CS student trying to get a program running using if/else and while for. I am using the debugger and stepping through the code. I can see that my "current_min" starts at 0 by default and doesn't change. I know I should grab the user input to define max/min, but when I try to put it in the for loop, it overwrites the max/min exiting with the max and min being the same number. With the current code it works, so long as there is a number less than 0 that goes in. What would be the best course of action here?

# python 3.7
current_max = int()
current_min = current_max

print("How many integers would you like to enter?")
number_of_ints = int(input())  # number of integers user would like to compare
print("Please enter "   str(number_of_ints)   " integers")  # requesting x integers based on user input on previous line

for numints in range(number_of_ints):
    number = int(input())
    if number > current_max:
        current_max = number
    elif number < current_min:
        current_min = number

print("min: ", current_min)
print("max: ", current_max)

CodePudding user response:

The only issue here is that you initialize your current_min and current_max to 0. The loop will thus check against 0 and only update the values if it is less than (or greater than) 0.

To fix, you should set your current_min and current_max to the first value that the user provides:

number = int(input())
current_min = number
current_max = number

for numints in range(numints-1):
    # your logic from above.

CodePudding user response:

You are very close. current_max and current_min should be initially equal and set to the first number entered just before the loop. Note that in Python, we don't declare variables as ints or anything -- your first two lines is the same thing as saying current_max = current_min = 0. Also note that as an argument to input you can specify a string which acts as the prompt that is written automatically to standard output for you without a trailing newline (so that input can be entered immediately after your string is written).

number_of_ints = int(input("How many integers would you like to enter? "))
print(f"Please enter {number_of_ints} integers")

number = int(input(f"Enter integer 1/{number_of_ints}: "))
cur_max = cur_min = number
for i in range(1, number_of_ints):
    number = int(input(f"Enter integer {i   1}/{number_of_ints}: "))
    if number > cur_max:
        cur_max = number
    elif number < cur_min:
        cur_min = number

print("min: ", cur_min)
print("max: ", cur_max)

Sample session:

How many integers would you like to enter? 5
Please enter 5 integers
Enter integer 1/5: -5
Enter integer 2/5: -1
Enter integer 3/5: 5
Enter integer 4/5: 9
Enter integer 5/5: 0
min:  -5
max:  9

Happy coding!

CodePudding user response:

The problem is right at the start, and can be seen in a debugger:

current_max = int()        # current_max is 0
current_min = current_max  # current_min is 0

Suppose we put in 2. Current min is 0, current max is 2. Suppose we then put in 3. Current min is zero, current max is 3. Unless a negative value is entered, current min will remain at zero. Moreover, if only negative values are entered, then current_max will be stuck at zero.

What I would do is add a boolean variable, initially false, to test if a value has been added already. On the first value, we update the minimum and maximum value to the new value.

# python 3.7
current_max = int()
current_min = current_max#
got_value=False

print("How many integers would you like to enter?")
number_of_ints = int(input())  # number of integers user would like to compare
print("Please enter "   str(number_of_ints)   " integers")  # requesting x integers based on user input on previous line

for numints in range(number_of_ints):
    number = int(input())
    if not got_value:
        current_min=number
        current_max= number
        got_value=True
    else:
        if number > current_max:
            current_max = number
        elif number < current_min:
            current_min = number

print("min: ", current_min)
print("max: ", current_max)
  • Related