Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:07:34+00:00 2026-06-15T22:07:34+00:00

What is wrong with my IF ELSE statement? IF NOT condition, do A. ELSE,

  • 0

What is wrong with my IF ELSE statement?

IF NOT condition, do A.
ELSE, do B.

But the result turns out quite different than I expected. :S

data['stock'] = ['0.02', '0.03', '0.04', '0.00', '0.05', '0.04', '0.05']

x = 0
y = len(data['Keywords'])

while x <= y - 1:
    if data['stock'][x] != 0:
        print data["stock"][x]
        a = a + 1
    else:
        print "hello"
        a = a + 1

Output:
0.02
0.03
0.04
0.00
0.05
0.04
0.05
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:07:35+00:00Added an answer on June 15, 2026 at 10:07 pm

    One obvious problem is that your list contains strings and your code expects numbers. In Python, you are allowed to compare 0 to "0" (they compare unequal).

    One way to fix it:

    data['stock'] = [0.02, 0.03, 0.04, 0.00, 0.05, 0.04, 0.05]
    

    Also, that loop looks decidedly un-Pythonic. The first step would be to rephrase it like so:

    for x in range(len(data['Keywords'])):
        if data['stock'][x] != 0:
            print data["stock"][x]
        else:
            print "hello"
    

    If you don’t use the value of x other than for indexing into the list, then the counter is unnecessary:

    for val in data["stock"]:
        if val != 0:
            print val
        else:
            print "hello"
    

    Note that this assumes that data["Keywords"] has the same length and data["stock"]. If that’s not the case, this code isn’t equivalent to yours.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Else statement not working properly in foreach loop? Here's my code. If something's wrong
Why does this condition always execute the ELSE and not the IF branch. Here's
This if else statement does not work: (in this situation it displays 'x is
I'm making a small program that uses a if else statement, but instead of
SOLVED: This is what was wrong: current.addFolder(folder); (in the final else clause of the
Whats wrong with my query? ALTER TABLE test_posts ADD sticky boolean NOT NULL default
I have a fairly simple if else statement in C# that looks something like
Hello guys! I have been trying to create a if/else statement code in jQuery
The following segment of code goes to the else statement when it should be
I hope this question does not seem vague but I am converting the old

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.