If-else statements not working in python?

I am working on a django project. Even when the condition is True, it doesn’t go to the elif statement My code :-

# views.py

# I have created this file - Harry

from django.http import HttpResponse from django.shortcuts import render

def index(request): return render(request, 'index.html')

# return HttpResponse("Home")

def analyze(request): #Get the text

djtext = request.POST.get('text')
print('got data', djtext)
removepunc = request.POST.get('removepunc', 'off')
fullcaps = request.POST.get('fullcaps', 'off')
newlineremover = request.POST.get('newlineremover', 'off')
extraspaceremover = request.POST.get('extraspaceremover', 'off')

# return HttpResponse(f'{djtext}, {removepunc}')
# analyzed = djtext
purpose = ''
if removepunc == 'on':
    punctuations =  '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    analyzed = ''
    for char in djtext:
        if char not in punctuations:
            analyzed += char
    djtext = analyzed
    purpose += 'Removed Punctuations '
    print('punctionatol', djtext)
elif fullcaps == 'on':
    print("Came here full of caps")
    purpose += 'Capitalized everything '

    analyzed = djtext.upper()
    djtext = analyzed
    print('Full caps', djtext)

elif newlineremover == 'on':
    analyzed = ''
    for char in djtext:
        if char != '\n':
            analyzed += char
    purpose += 'Removed new lines'
    djtext = analyzed
    print('new oiens', djtext)

elif extraspaceremover == 'on':
    analyzed = ''
    for index, char in enumerate(djtext):
        if  not (djtext[index] == ' ' and  djtext[index + 1] == ' '):
            analyzed = analyzed+char
    djtext = analyzed
    print('spaxes', djtext)

    purpose = 'No task given'
    return HttpResponse("Choose a task to be performed and try again")
djtext = analyzed       
print('final dj sfu k  text', djtext)
params = { 
#Analyze the text
return render(request, 'analyze.html', params)

I am very frustrated help Thanks

try printing all variables removepunc, fullcaps, newlineremover, extraspaceremover etc, It will be printed to the console where you run py manage.py runserver when you try to access the page…
It will give you idea, what’s wrong with the code…

I used this method and got the answer. I just changed them all to its. So that all conditions are checked for. Thanks for answering😃.
I thought no one would be patient enough to read this much code. Like me. Thanks

I read it because of curiosity, because I hardly face difficulty with if/else statements…
Any of the bug, or this type of mistakes help me so that I should not repeat them, or should solve them easily…:grin::grin:


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like