IDontKnowReally
Badges: 11
Rep:
?
#1
Report Thread starter 2 years ago
#1
When i use 'and' in an if loop, it works like how you'd expect 'or' to work. and I dont understand why?
0
reply
Ryanthom100
Badges: 21
Rep:
?
#2
Report 2 years ago
#2
Da ****, this is a new one i have never heard of lol. Are you sure you fully understand what each operator does?
1
reply
Blue_Cow
Badges: 22
Rep:
?
#3
Report 2 years ago
#3
(Original post by IDontKnowReally)
When i use 'and' in an if loop, it works like how you'd expect 'or' to work. and I dont understand why?
What on earth is an if loop?

Post your code onto Pastebin and paste the link on here as a reply so we can look at the section of code in question.
0
reply
Ryanthom100
Badges: 21
Rep:
?
#4
Report 2 years ago
#4
(Original post by Blue_Cow)
What on earth is an if loop?
Maybe he means a nested if statement withing a while loop, he could be a little clearer on that though haha
0
reply
qasim-96
  • Political Ambassador
Badges: 15
Rep:
?
#5
Report 2 years ago
#5
Are you sure there's such a thing as an if loop?
0
reply
Blue_Cow
Badges: 22
Rep:
?
#6
Report 2 years ago
#6
(Original post by Ryanthom100)
Maybe he means a nested if statement withing a while loop, he could be a little clearer on that though haha
Yeah. That was a load of jumbled up mess...
1
reply
IDontKnowReally
Badges: 11
Rep:
?
#7
Report Thread starter 2 years ago
#7
(Original post by Ryanthom100)
Da ****, this is a new one i have never heard of lol. Are you sure you fully understand what each operator does?
Im not sure I do tbh.
basically i want to do the following:
if condition A (x > y) and condition B (z < a) are both met then I want to exit the while loop that the' if ' statement is in
if x>y and z <a :
break
but it seems to break even when just z<a is met but not x>y
0
reply
IDontKnowReally
Badges: 11
Rep:
?
#8
Report Thread starter 2 years ago
#8
(Original post by Blue_Cow)
What on earth is an if loop?

Post your code onto Pastebin and paste the link on here as a reply so we can look at the section of code in question.
(Original post by qasim-96)
Are you sure there's such a thing as an if loop?
(Original post by Blue_Cow)
Yeah. That was a load of jumbled up mess...
sorry its been a long day, I meant a nested if statement in a while loop
1
reply
NatKH
Badges: 13
Rep:
?
#9
Report 2 years ago
#9
(Original post by IDontKnowReally)
Im not sure I do tbh.
basically i want to do the following:
if condition A (x > y) and condition B (z < a) are both met then I want to exit the while loop that the' if ' thing is in
if x>y and z <a :
break
but it seems to break even when just z<a is met but not x>y
Are you using a boolean? Set variable = True and then variable = False if the condition is met to exit while loop.
2
reply
Ryanthom100
Badges: 21
Rep:
?
#10
Report 2 years ago
#10
(Original post by IDontKnowReally)
Im not sure I do tbh.
basically i want to do the following:
if condition A (x > y) and condition B (z < a) are both met then I want to exit the while loop that the' if ' statement is in
if x>y and z <a :
break
but it seems to break even when just z<a is met but not x>y
^^ use boolean, i never use breaks tbh, i just tend to use boolean
0
reply
IDontKnowReally
Badges: 11
Rep:
?
#11
Report Thread starter 2 years ago
#11
(Original post by NatKH)
Are you using a boolean? Set variable = True and then variable = False if the condition is met to exit while loop.
Im not quite sure what you mean?
0
reply
NatKH
Badges: 13
Rep:
?
#12
Report 2 years ago
#12
(Original post by IDontKnowReally)
Im not quite sure what you mean?
I'll write out the pseudocode just to give you an idea

boolName = True

while boolName == True do
if x > y and z < a then
boolName = False
endif
endwhile
0
reply
IDontKnowReally
Badges: 11
Rep:
?
#13
Report Thread starter 2 years ago
#13
(Original post by NatKH)
I'll write out the pseudocode just to give you an idea

boolName = True

while boolName == True do
if x > y and z < a then
boolName = False
endif
endwhile
AHh okay I see what you mean.
I cant really use booleans because I want the code in the while loop to repeat until the value of a variable becomes less than 0
0
reply
bnutt
Badges: 5
Rep:
?
#14
Report 2 years ago
#14
try putting brackets.

(z > y) and (z < a)
0
reply
IDontKnowReally
Badges: 11
Rep:
?
#15
Report Thread starter 2 years ago
#15
(Original post by bnutt)
try putting brackets.

(z > y) and (z < a)
I just tried this.
FOr some reason, it still breaks when only z<a is met
0
reply
bnutt
Badges: 5
Rep:
?
#16
Report 2 years ago
#16
post the full code, you must be doing something else wrong
0
reply
mhsc
Badges: 17
Rep:
?
#17
Report 2 years ago
#17
put in break points and step through your code line by line with a debugger
0
reply
winterscoming
Badges: 19
Rep:
?
#18
Report 2 years ago
#18
(Original post by IDontKnowReally)
I just tried this.
FOr some reason, it still breaks when only z<a is met
It sounds like you're at a point now where you're just "stuck" and poking around in the dark a bit now, which is a fairly normal reaction when you can't see an obvious reason why your program is doing what it's doing, but it's also not the right approach - it'll just lead to more frustration and a lot of time/effort walking around in circles.

The other thing which is fairly normal when you're learning to program for the first time is that you probably haven't seen things like this happen before - and therefore probably don't know how to start troubleshooting things like this.

So as the post above me mentioned - learn how to use your debugger. A debugger is just about the most important tool for programming - it takes a few minutes to learn how to do it, and it'll save you a lot of frustration. If you're using an IDE instead of the command-line (e.g. Eclipse, Visual Studio, etc), then it should be as simple as clicking on a line to set a breakpoint.

If you run your program in a debugger and spend a few minutes stepping through, you'd probably notice the problem fairly quickly.

Alternatively, try pasting a snippet of your code into this Python Code visualiser where you can watch what's happening step-by-step:

http://www.pythontutor.com/visualize.html#mode=edit

Simple Example with some code: https://goo.gl/n3S3Sb(click the "Visualise Execution" button then press the 'Forward >' button a few times to see what it's doing... )

If you're not using a debugger, then the alternative is to add a bunch of print statements which show what's happening, but the debugger will suspend the program at each step along the way, so it's a lot easier to see what's going on than reading back through a list of print statements.
1
reply
veekm
Badges: 8
Rep:
?
#19
Report 2 years ago
#19
foo bar - check imageAttachment 719764
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • The University of Law
    Solicitor Series: Assessing Trainee Skills – LPC, GDL and MA Law - Exeter campus Postgraduate
    Thu, 27 Feb '20
  • University of East Anglia
    PGCE Open day Postgraduate
    Sat, 29 Feb '20
  • Edinburgh Napier University
    Postgraduate Drop-in Brunch Postgraduate
    Sat, 29 Feb '20

Do you get study leave?

Yes- I like it (277)
61.15%
Yes- I don't like it (22)
4.86%
No- I want it (120)
26.49%
No- I don't want it (34)
7.51%

Watched Threads

View All
Latest
My Feed