0

I'm a beginner in coding and theres something wrong with my elif statement and i cant figure out what

For some reason this code gets stuck in the or, it will never execute the '4' option. We're trying to use less repetitive code, but nothing too advanced because we just starting learning python.

# defines menu
menu = '1- Sum of integers\n2- Sum of squares\n3- sum of cubes\n4- Geometric series'

# asks user for menu choice
print(menu)

menuChoice = 0
while menuChoice >= 0:
menuChoice = int(input(
    'Please enter a choice from our menu. (enter a negative number to quit)\n'))
sum = 0
calcSum = 0
if menuChoice == 0:
    print(menu)
elif menuChoice == 1 or 2 or 3:
    n = int(input('Enter n: '))
    for i in range(n + 1):
        if menuChoice == 1:
            sum += i
            calcSum = n * (n + 1)/2
        elif menuChoice == 2:
            sum += i ** 2
            calcSum = n * (n + 1) * (2 * n + 1)/6
        elif menuChoice == 3:
            sum += i ** 3
            calcSum = (n + (n + 1) / 2) ** 2

elif menuChoice == 4:
    x = int(input("Please Enter the Common Ratio: "))
    n = int(input("Please Enter the Total Numbers in this Geometric Series:  "))
    for i in range(n + 1):
        sum += x ** i
mordye137
  • 15
  • 6
  • `or 2 or 3` translates as `or True or True` since non-zero values are “truthy”. You want `menuChoice == 1 or menuChoice == 2 or menuChoice == 3` or simpler `menuChoice in (1, 2, 3)` – AJNeufeld Sep 23 '19 at 00:53
  • `menuChoice == 1 or 2 or 3` doesn't do what you think. – lurker Sep 23 '19 at 00:53
  • Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Carcigenicate Sep 23 '19 at 01:15

2 Answers2

1

menuChoice == 1 or 2 or 3 is parsed as (menuChoice == 1) or 2 or 3, and always evaulates to a truthy value because 2 (and 3) is truthy. Use menuChoice == 1 or menuChoice == 2 or menuChoice == 3 or menuChoice in (1, 2, 3) instead.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
0

Change your if statment with this:

elif menuChoice == 1 or menuChoice == 2 or menuChoice == 3:   

Yuor code didn't work because in python or 2 or 3 condition translates as or True or True since the values (non-zero) ​​are always considered 'truthful' in the conditions.

This code run:

# defines menu
menu = '1- Sum of integers\n2- Sum of squares\n3- sum of cubes\n4- Geometric series'

# asks user for menu choice
print(menu)

menuChoice = 0
while menuChoice >= 0:
    menuChoice = int(input('Please enter a choice from our menu. (enter a negative number to quit)\n'))
    sum = 0
    calcSum = 0
    if menuChoice == 0:
        print(menu)
    elif menuChoice == 1 or menuChoice == 2 or menuChoice == 3:
        n = int(input('Enter n: '))
        for i in range(n + 1):
            if menuChoice == 1:
                sum += i
                calcSum = n * (n + 1)/2
            elif menuChoice == 2:
                sum += i ** 2
                calcSum = n * (n + 1) * (2 * n + 1)/6
            elif menuChoice == 3:
                sum += i ** 3
                calcSum = (n + (n + 1) / 2) ** 2

    elif menuChoice == 4:
        x = int(input("Please Enter the Common Ratio: "))
        n = int(input("Please Enter the Total Numbers in this Geometric Series:  "))
        for i in range(n + 1):
            sum += x ** i
Massifox
  • 4,369
  • 11
  • 31