mordye137
mordye137

Reputation: 15

If statement wont execute last line

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

Upvotes: 0

Views: 59

Answers (2)

Massifox
Massifox

Reputation: 4487

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

Upvotes: 0

Solomon Ucko
Solomon Ucko

Reputation: 6109

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.

Upvotes: 1

Related Questions