d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python Help - Basic, But Its Just Not Clicking > While Loops
Add Reply New Topic New Poll
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Oct 18 2015 04:00pm


Here is what I'm trying to do -

Create a calculator program that allows the user to input their operator (+,-,/,*,**,%) and two numbers, and it prints their result. At the end, they get to input 0 to begin at the start again, or 1 to end.

I can't get it to loop, and when I've tried to get it to loop, it does an infinite loop, and I have to control C to get out of it.

I've pm'd a few people, and they've been helpful, but I want to post this code and just have someone tell me how much of a dumbass I am. I'm in week 3 of my freshman year, and I have no prior experience, so this is all just frustrating for me right now :P
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 18 2015 05:52pm
Quote
I've pm'd a few people, and they've been helpful,

create posts, not pms. that way people dont keep repeating themselves, and more people are available to help.

first problem is this line:
Code
if yes = 0

when checking for equality, always use ==

the main issue is you have to think about the loop. it should follow this kind of pattern:

Code
condition = True
while condition:
# loop body here
condition = test_loop_condition()
# end of loop


everything you want to repeat should be inside the loop. you really want to do a "do-while" loop, but i dont think python has it, so this is a pattern to simulate it. essentially we force the loop to execute once by making the condition true at the beginning.

This post was edited by carteblanche on Oct 18 2015 05:53pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 18 2015 08:53pm
Quote (carteblanche @ Oct 18 2015 06:52pm)
create posts, not pms. that way people dont keep repeating themselves, and more people are available to help.

first problem is this line:
Code
if yes = 0

when checking for equality, always use ==

the main issue is you have to think about the loop. it should follow this kind of pattern:

Code
condition = True
while condition:
# loop body here
condition = test_loop_condition()
# end of loop


everything you want to repeat should be inside the loop. you really want to do a "do-while" loop, but i dont think python has it, so this is a pattern to simulate it. essentially we force the loop to execute once by making the condition true at the beginning.


unless it is VB.NET
Retired Moderator
Posts: 38,135
Joined: May 27 2006
Gold: 3,835.50
Trader: Trusted
Oct 18 2015 09:57pm
= Assigns
== Compares

You have my PM, but post #2 is the same

This post was edited by ArtofApocalypse on Oct 18 2015 09:58pm
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Oct 19 2015 11:31am
yes is expecting an int based on int(intput()), but your code suggests comparison to a str.

On your while loop you're missing the colon at the end of the first line.

Also, as others pointed out = is for referencing variables, == is for Boolean comparison.

This post was edited by j0ltk0la on Oct 19 2015 11:32am
Member
Posts: 29,367
Joined: Mar 27 2008
Gold: 504.69
Oct 20 2015 05:45am
You need to emulate a do while loop using a Boolean variable. You should avoid using breaks unless you are in switch selection statement. It works but its sloppy.

A Boolean variable is a variable that only hold two values, true or false.

This is not formatted right but,

Boolean condition;

While (condition)
{
Your code
Condition = test_condition ();
}

You will nest your entire program in the while loop and test condition will return a true to continue or a false to exit.


This post was edited by ROM on Oct 20 2015 05:46am
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Oct 21 2015 06:10am
Quote (ROM @ Oct 20 2015 05:45am)
You need to emulate a do while loop using a Boolean variable. You should avoid using breaks unless you are in switch selection statement. It works but its sloppy.

A Boolean variable is a variable that only hold two values, true or false.

This is not formatted right but,

Boolean condition;

While (condition)
{
Your code
Condition = test_condition ();
}

You will nest your entire program in the while loop and test condition will return a true to continue or a false to exit.


Python doesn't have do while technically and it doesn't have case-switch either, but logically things should work out okay.

With the way Python and most languages should work, 0 will come back is false, so you can reference anything to 0 and check with bool() to see if False.

Code
>>> a = 0
>>> b = 1
>>> bool(a)
False
>>> bool(b)
True
>>>


I'm not sure how far you are with Python, if you're allowed to use functions or not, but generally things start to make more sense with the introduction of those.

Since I believe that you're wanting to have your whole program looping, having your code separated into a function or functions and setting these up in the loop while probably give you what you need.

Code
def main():
# all your code
# prompt for input
# calculate
# ask if you want to continue or make separate continue() function

def continue():
# ask if want to continue
# return True or False

while continue():
main()
# exits when continue() == False
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Oct 21 2015 12:08pm
Quote (j0ltk0la @ Oct 21 2015 06:10am)
Python doesn't have do while technically and it doesn't have case-switch either, but logically things should work out okay.

With the way Python and most languages should work, 0 will come back is false, so you can reference anything to 0 and check with bool() to see if False.

Code
>>> a = 0
>>> b = 1
>>> bool(a)
False
>>> bool(b)
True
>>>


I'm not sure how far you are with Python, if you're allowed to use functions or not, but generally things start to make more sense with the introduction of those.

Since I believe that you're wanting to have your whole program looping, having your code separated into a function or functions and setting these up in the loop while probably give you what you need.

Code
def main():
# all your code
# prompt for input
# calculate
# ask if you want to continue or make separate continue() function

def continue():
# ask if want to continue
# return True or False

while continue():
main()
# exits when continue() == False


Bad advice on naming it continue, it's a reserved term, but same idea.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll