-2

I have decided to learn python coming from a c++ background and i am trying to create a simple program to print odd numbers and calculate the sum of even numbers in a range given from the user.

while the code works correctly for some test cases my while loop checking if the first number is greater then the second number does not work as intended.

Here is the code:

number1= input("enter number 1: ") #line 5 error
number2= input("enter number 2: ") 
number_list = []

while number1 >= number2:
    number1= input("please enter a number: ")
    number2= input("please enter a number larger than number 1: ")

for number in range(int(number1),int(number2)+1):
    if number % 2 != 0:
        print(number)
    else:
        number_list.append(number)

sum = sum(number_list)
print(f"the sum of all even numbers is {sum}")

for the given input (1,5) the output is correct ( 1,3,5) and sum is 6 but for any other input where the first number is not 1 i get stuck in a infinite loop from line 5.

with the input (2,10) -> infinite loop with the input (30,100) -> infinite loop until the input is a 1

not sure why it is not working as intended and think the logic is correct but maybe i am using c++ syntax?

EDIT: input (2,5) works correctly but (2,10) does not

any help would be appreciated

william_
  • 1,131
  • 6
  • 20
  • 2
    Your inputs are strings. Strings are ordered lexicographically, so `'2'` > `'10'`. – khelwood Jul 10 '20 at 08:10
  • 1
    Convert your inputs to `int` at creation `int(input("enter number 1: "))` – Trenton McKinney Jul 10 '20 at 08:10
  • 1
    See [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) to correctly ask for valid input. It's not efficient to ask for input b4 the while-loop. – Trenton McKinney Jul 10 '20 at 08:18

4 Answers4

1

Because you are comparing string instead of integers. You can read more about string comparison from here.

Replace,

while number1 >= number2:

with,

while int(number1) >= int(number2):
shaik moeed
  • 5,300
  • 1
  • 18
  • 54
1

input() in python returns a string no matter you've passed a number or a word. So, you need to typecast it to an integer to get the desired result. You are correctly typecasting the input in your for loop but have missed to do so in while loop. Just change it to the following:

while int(number1) >= int(number2):
Moosa Saadat
  • 1,159
  • 1
  • 8
  • 20
1
  1. Incorrectly validating the inputs
    • The inputs are str type
      • while number1 >= number2: is comparing strings
        • This should not be used as the condition of the while-loop
    • Only code for the inputs once
    • Validate the inputs are int
    • Validate number1 < number2
  2. There's no reason to make evens a list, and then sum it
    • sum a running total with evens += v
  3. You are incorrectly using a python function, sum, as a variable name sum = sum(number_list)
while True:
    try:  # confirms input is int
        number1 = int(input("enter number 1: "))
        number2 = int(input("enter a number greater than number 1: "))
    except ValueError:  # ask for input if not int
        continue
    if number1 < number2:  # valid input number2 is greater than number1 so exits while loop
        break
        
evens = 0  # initialize evens
for v in range(number1, number2 + 1):  # iterate through range of input values
    if v % 2 != 0:  # check if value is odd
        print(v)
    else:
        evens += v  # add v to evens

print(f'the sum of all even numbers is {evens}')
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0

Your problem in is that in your while loop is comparing strings instead of integers. For For example: '2' >= '10' => True, resulting in a unexpected iteration of the loop.

You can fix your code typecasting the result of input to int:

number1= int(input("enter number 1: ")) # typecast to int
number2= int(input("enter number 2: ")) # typecast to int
number_list = []

while number1 >= number2:
        number1= input("please enter a number: ")
        number2= input("please enter a number larger than number 1: ")

for number in range(number1,number2+1): # removed typecast
    if number % 2 != 0:
        print(number)
    else:
        number_list.append(number)

sum = sum(number_list)
print(f"the sum of all even numbers is {sum}")
jpuriol
  • 362
  • 2
  • 14