0

I am writing a program to calculate the sum of odd and even numbers between 1-50.

This is what I have so far:

Display the message

print("Sums of odd and and even numbers")
print("="*45)

Ask the user whether they want to continue or not.

ch = 'y'

while(ch == 'y'):

    # Prompt the user to enter a number.

    # Store the number entered by the user in the variable n.

    num =int(input('Enter a whole number between 1 and 50: '))
    

    # If the number entered by the user is in the range 1-50

    while(num<1 or num>50):
      num = int(input("Try again. Your number must be between 1 and 50: "))

    # Check if the valid number entered by the user is even or odd.

    # If number mod 2 == 0, then the number is even

    if num%2 == 0:
      # Print the message.
      print('')
      print('Your number is an even number. ')

      # Initialize the variable even_sum to 0.
      # This variable is used to store sum of even numbers

      even_sum=0

      # for loop to find sum of even numbers
      for i in range(2,num+1,2):
        even_sum+=i

      # Print the sum of the even numbers from 2 till the number
      # entered by the user.
      print('The sum of the even numbers from 2 to %d is : '%num,even_sum)
    else:
      # If number mod 2 is not equal to 0, print the message.
      # So, the number is odd.
      print('')
      print('Your number is an odd number.')

      # Initialize the variable odd_sum to 0.
      # This variable is used to store sum of odd numbers

      odd_sum=0

      # for loop to find sum of odd numbers
      for i in range(1,num+1,2):
        odd_sum+=i

      # Print the sum of the odd numbers from 1 till the number
      # entered by the user.
      print('The sum of odd numbers from 1 to %d is: '%num,odd_sum)
      print('')

    # Ask the user whether he/she wants to continue the program.

    ch = input("\n Try again? (y/n) ")

The program works but I just need to add a loop that checks If num is not and integer. Like if the num is a floating point number or a string, then prompt again to input a whole number between 1 and 50

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 1
    you cast input to int already: `int(input("Try again. Your number must be between 1 and 50: "))` – depperm Feb 23 '22 at 13:52
  • 2
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Dharman Mar 22 '22 at 23:42

2 Answers2

1

You could use the isdigit() method for strings, because the input returns always an string.

user_input = input("Enter something:")
    if user_input.isdigit():
        print("Is a number")
    else:
        print("Not a number")
RubyLearning
  • 83
  • 1
  • 7
0

You could do it like this:

ch = 'y'

while(ch == 'y'):
    num = 2
    # Prompt the user to enter a number.
    # Store the number entered by the user in the variable n.
    while True:
        if isinstance(num, int):
            if num<1 or num>50:
                num = input('Try again. Your number must be between 1-50: ')
            else:
                num = input('Enter a whole number between 1 and 50: ')
        else:
            num = input('Enter a whole number between 1 and 50: ')
        try:
            num = int(num)
            if num<1 or num>50:
                continue
            break
        except ValueError:
            print("You did not enter a number, try again.")

    # Check if the valid number entered by the user is even or odd.
    # If number mod 2 == 0, then the number is even
    if num%2 == 0:
      # Print the message.
      print('')
      print('Your number is an even number. ')

      # Initialize the variable even_sum to 0.
      # This variable is used to store sum of even numbers

      even_sum=0

      # for loop to find sum of even numbers
      for i in range(2,num+1,2):
        even_sum+=i

      # Print the sum of the even numbers from 2 till the number
      # entered by the user.
      print('The sum of the even numbers from 2 to %d is : '%num,even_sum)
    else:
      # If number mod 2 is not equal to 0, print the message.
      # So, the number is odd.
      print('')
      print('Your number is an odd number.')

      # Initialize the variable odd_sum to 0.
      # This variable is used to store sum of odd numbers

      odd_sum=0

      # for loop to find sum of odd numbers
      for i in range(1,num+1,2):
        odd_sum+=i

      # Print the sum of the odd numbers from 1 till the number
      # entered by the user.
      print('The sum of odd numbers from 1 to %d is: '%num,odd_sum)
      print('')

    # Ask the user whether he/she wants to continue the program.

    ch = input("\n Try again? (y/n) ")

Test:

Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1

Your number is an odd number.
The sum of odd numbers from 1 to 1 is:  1


 Try again? (y/n) y
Enter a whole number between 1 and 50: 10

Your number is an even number.
The sum of the even numbers from 2 to 10 is :  30

 Try again? (y/n) n
Cow
  • 2,543
  • 4
  • 13
  • 25