Ask coding questions

← Back to all posts
Nested IF/ While loop problem
Lanna (36)

I think I'm starting to get annoying now.
Sorry.
Yeah about the question, is it possible to create a "while loop" that consists of a nested if?
Language: Python

Answered by PYer (2755) [earned 5 cycles]
View Answer
Commentshotnewtop
PYer (2755)

Yes, of cours!

a = 1
while True:
    if a > 5:
        print("A is greater than 5.")
    else:
        print('A is not greater than 5.')

This is a totally useless and simple example, but you can make them more complicated.

Lanna (36)

@PYer Thanks.
I still have another doubt though.

Why isn't this working? Help is appreciated:)

PYer (2755)

@Lanna You need to call the function. isdigit is not a variable, but a method, that returns True or False. To "call" it, you would add () to the function. The while loop line should look like this: while marks.isdigit(). Other than that I don't see anything wrong.

Lanna (36)

@PYer So there doesn't have to be anything inside the brackets?

joshwood (116)

@Lanna Nope, you can leave them empty.

Lanna (36)

@joshwood Yeah got it. Thanks!

coderash (70)

You can do something like this:
x=0;
While x=0:
print("Hello!")

Lanna (36)

Thanks a lot for the help guys! I kinda figured it out now, thanks again!!👍👍

Lanna (36)


I tried it out on Portable Python and it doesn't seem to work. I have a theory that,possibly, since I have converted input into float value, the programme will consider marks as decimal always. If wrong, please help!

a5rocks (732)

@Lanna is isdigit a function? As in, do you need to call it? Because you are not calling it, and that might be why your program isnt working.

Lanna (36)

@a5rocks What do you mean by "calling it"?

ibbi (32)

@Lanna It's usually helpful to include the code in a repl, so we can mess with it to figure out what's wrong :)

From what I see here, there are two problems. The first is you are already converting what the user inputs into a float, so the variable marks will be a float. If python cannot convert marks to a float, it will throw an error (ie if the user does not input a number).

So, marks is already a float. The isdigit function only exists for strings, and checking marks.isdigit will throw an error because marks is now a float (because you converted it earlier with float(blabla)!).

Another really small problem, which I think is what @a5rocks was referring to, is you need to call the isdigit function. This means instead of writing marks.isdigit, you must write marks.isdigit(). Leaving out the parenthesis does weird things! I think it will return the address of the function, instead of running it.

Let me know if anything is not clear!

Lanna (36)

@ibbi Thanks, I'll try to code in repl.it, more often. Yeah, I kind of understand now! Thanks a lot!!