- **Python**
(*http://www.velocityreviews.com/forums/f43-python.html*)

- - **Else statement executing when it shouldnt**
(*http://www.velocityreviews.com/forums/t956741-else-statement-executing-when-it-shouldnt.html*)

Else statement executing when it shouldntan else statement is running when it shouldnt be. It is on the last line. Whenever i am in the math or game function, when i type in main, it goes back to the start of the program, but it also says not a valid function. I am stumped!
Here is my code: #Cmd #Created By Eli M. #import modules import random import math gtn = 0 print ("Type in help for a list of cmd functions") #initiate main loop cmd = 0 while cmd == 0: #ask for input on function function = raw_input("Type in a function:") #start math loop if function == "math": run = 0 while run == 0: #ask for math operation type = raw_input("What math operation do you want to use?") if type == "multiplication": x = raw_input("Type in your first number:") y = raw_input("Multiply your first number by:") try: ans = int(x) * int(y) print (ans) try: ans = float(x) * float(y) print (ans) except ValueError, err: print ("Not a valid number") except OverflowError, err: print ("Number too large") #division math function if type == "division": x = raw_input("Type in your first number:") y = raw_input("Divide your first number by:") try: ans = float(x) / float(y) print (ans) except ZeroDivisionError, err: print ("Can't divide by zero") except ValueError, err: print ("Not a valid number") except OverflowError, err: print ("Number too large") #subtraction math function if type == "subtraction": x = raw_input("Type in your first number:") y = raw_input("Subtract your first number by:") try: ans = float(x) - float(y) print (ans) except ValueError, err: print ("Not a valid number") #addition math function if type == "addition": x = raw_input("Type in your first number:") y = raw_input("Add your first number by:") try: ans = float(x) + float(y) print (ans) except ValueError, err: try: ans = int(x) + int(y) print (ans) except ValueError, err: print ("Not a valid number") except OverflowError, err: print ("Number too large") #square root math function if type == "square root": x = raw_input("Type in your number:") try: y = float(x) z = math.sqrt(y) print (z) except ValueError, err: print ("Not a valid number") except OverflowError, err: print ("Number too large") #to the power of... math function if type == "power": x = raw_input("Type in your number:") y = raw_input("Multiply your first number by the power of:") try: ans = float(x) ** float(y) print (ans) except OverflowError, err: print ("Number too large") except ValueError, err: print ("Not a valid number") #break the math loop if type == "main": run = 1 #absolute value math function if type == "absolute value": try: x = float(raw_input("Type in your number:")) y = math.fabs(x) print (y) except ValueError, err: print ("Not a valid number") if function == "random number": try: x = int(raw_input("Minimum number:")) y = int(raw_input("Maximum number:")) num = random.randint(x, y) print (num) except ValueError, err: print ("Not a valid number") if function == "games": games = 0 while games == 0: gamechoice = raw_input("What game do you want to play:") if gamechoice == "guess the number": run = 0 while run == 0: print ("I am thinking of a number between 1 and 20") num = random.randint(1, 20) num = int(num) guesses = 0 guessestaken = 0 while guesses == 0: try: guess = raw_input("Your guess:") guess = int(guess) guessestaken = (guessestaken) + 1 guessestaken = int(guessestaken) if guess == (num): print 'Correct! It took you', int(guessestaken), 'guesses!' playagain = raw_input("Do you want to play again?") if playagain == "yes": guesses = 1 if playagain == "no": run = 1 guesses = 1 if guess > num: print ("My number is lower") if guess < num: print ("My number is higher") except TypeError, err: print ("Not a valid number") if gamechoice == "main": games = 1 #help function if function == "help": helpfunc = 0 while helpfunc == 0: #show functions print ("Functions:") print ("Math: multiplication, division, subtraction, addition, square root, power, absolute value") print ("Random Number") print ("Games: Guess the number") helpmain = raw_input("Type in main to go back") if helpmain == "main": #end helpfunction loop helpfunc = 1 cmd = 0 else: print ("Not a valid function") |

Re: Else statement executing when it shouldntIn article <2cc6791f-ba56-406c-a5b0-b23023caf4bb@googlegroups.com>,
eli m <techgeek201@gmail.com> wrote: > an else statement is running when it shouldnt be. It is on the last line. > Whenever i am in the math or game function, when i type in main, it goes back > to the start of the program, but it also says not a valid function. I am > stumped! > Here is my code: [many lines of code elided] TL;DNR :-) A basic debugging technique is to try to find the minimum amount of code that can reproduce the problem. Find some hunk of lines that you're pretty sure can't be at fault, and delete them. See if you can still reproduce the problem. Assuming you can, delete another hunk of code. Keep going until you're down to the smallest possible amount of code which demonstrates the problem. There's a couple of good things that come out of this. One is that it's likely that in the course of doing this, you'll figure out what's wrong. The other is that if you can't figure it out, at least now you'll have something that's easy to show somebody else when you ask for help. |

Re: Else statement executing when it shouldntOn Mon, Jan 21, 2013 at 3:40 PM, eli m <techgeek201@gmail.com> wrote:
> an else statement is running when it shouldnt be. It is on the last line. Whenever i am in the math or game function, when i type in main, it goes back to the start of the program, but it also says not a valid function. I am stumped! Check your indentation levels. I see a few things here that look odd: > if function == "help": > while helpfunc == 0: > if helpmain == "main": > else: What is the else meant to bind to? The innermost if? The 'if function == "help"'? It's currently binding to the while. Recommendation: Break this up! Your main loop is huge! It's way too easy to get lost in it. And while you're at it, consider unifying some of the similar blocks of code. The solution to both issues is simple: Use functions. Have you been taught about them yet? Also, side tip: Be honest about homework. I'm fairly sure that's what this is. :) ChrisA |

Re: Else statement executing when it shouldntOn Sunday, January 20, 2013 8:40:47 PM UTC-8, eli m wrote:
hint: Use the comments in the code to find out where my error is. > > Here is my code: > > #Cmd > > #Created By Eli M. > > #import modules > > import random > > import math > > gtn = 0 > > print ("Type in help for a list of cmd functions") > > #initiate main loop > > cmd = 0 > > while cmd == 0: > > #ask for input on function > > function = raw_input("Type in a function:") > > #start math loop > > if function == "math": > > run = 0 > > while run == 0: > > #ask for math operation > > type = raw_input("What math operation do you want to use?") > > if type == "multiplication": > > x = raw_input("Type in your first number:") > > y = raw_input("Multiply your first number by:") > > try: > > ans = int(x) * int(y) > > print (ans) > > try: > > ans = float(x) * float(y) > > print (ans) > > except ValueError, err: > > print ("Not a valid number") > > except OverflowError, err: > > print ("Number too large") > > #division math function > > if type == "division": > > x = raw_input("Type in your first number:") > > y = raw_input("Divide your first number by:") > > try: > > ans = float(x) / float(y) > > print (ans) > > except ZeroDivisionError, err: > > print ("Can't divide by zero") > > except ValueError, err: > > print ("Not a valid number") > > except OverflowError, err: > > print ("Number too large") > > #subtraction math function > > if type == "subtraction": > > x = raw_input("Type in your first number:") > > y = raw_input("Subtract your first number by:") > > try: > > ans = float(x) - float(y) > > print (ans) > > except ValueError, err: > > print ("Not a valid number") > > #addition math function > > if type == "addition": > > x = raw_input("Type in your first number:") > > y = raw_input("Add your first number by:") > > try: > > ans = float(x) + float(y) > > print (ans) > > except ValueError, err: > > try: > > ans = int(x) + int(y) > > print (ans) > > except ValueError, err: > > print ("Not a valid number") > > except OverflowError, err: > > print ("Number too large") > > #square root math function > > if type == "square root": > > x = raw_input("Type in your number:") > > try: > > y = float(x) > > z = math.sqrt(y) > > print (z) > > except ValueError, err: > > print ("Not a valid number") > > except OverflowError, err: > > print ("Number too large") > > > > #to the power of... math function > > if type == "power": > > x = raw_input("Type in your number:") > > y = raw_input("Multiply your first number by the power of:") > > try: > > ans = float(x) ** float(y) > > print (ans) > > except OverflowError, err: > > print ("Number too large") > > except ValueError, err: > > print ("Not a valid number") > > #break the math loop > > if type == "main": > > run = 1 > > #absolute value math function > > if type == "absolute value": > > try: > > x = float(raw_input("Type in your number:")) > > y = math.fabs(x) > > print (y) > > except ValueError, err: > > print ("Not a valid number") > > if function == "random number": > > try: > > x = int(raw_input("Minimum number:")) > > y = int(raw_input("Maximum number:")) > > num = random.randint(x, y) > > print (num) > > except ValueError, err: > > print ("Not a valid number") > > if function == "games": > > games = 0 > > while games == 0: > > gamechoice = raw_input("What game do you want to play:") > > if gamechoice == "guess the number": > > run = 0 > > while run == 0: > > print ("I am thinking of a number between 1 and 20") > > num = random.randint(1, 20) > > num = int(num) > > guesses = 0 > > guessestaken = 0 > > while guesses == 0: > > try: > > guess = raw_input("Your guess:") > > guess = int(guess) > > guessestaken = (guessestaken) + 1 > > guessestaken = int(guessestaken) > > if guess == (num): > > print 'Correct! It took you', int(guessestaken), 'guesses!' > > playagain = raw_input("Do you want to play again?") > > if playagain == "yes": > > guesses = 1 > > if playagain == "no": > > run = 1 > > guesses = 1 > > if guess > num: > > print ("My number is lower") > > if guess < num: > > print ("My number is higher") > > except TypeError, err: > > print ("Not a valid number") > > if gamechoice == "main": > > games = 1 > > > > #help function > > if function == "help": > > helpfunc = 0 > > while helpfunc == 0: > > #show functions > > print ("Functions:") > > print ("Math: multiplication, division, subtraction, addition, square root, power, absolute value") > > print ("Random Number") > > print ("Games: Guess the number") > > helpmain = raw_input("Type in main to go back") > > if helpmain == "main": > > #end helpfunction loop > > helpfunc = 1 > > cmd = 0 > > else: > > print ("Not a valid function") |

Re: Else statement executing when it shouldntOn Sunday, January 20, 2013 8:52:12 PM UTC-8, Chris Angelico wrote:
> On Mon, Jan 21, 2013 at 3:40 PM, eli m <techgeek201@gmail.com> wrote: > > > an else statement is running when it shouldnt be. It is on the last line. Whenever i am in the math or game function, when i type in main, it goes back to the start of the program, but it also says not a valid function. I am stumped! > > > > Check your indentation levels. I see a few things here that look odd: > > > > > if function == "help": > > > while helpfunc == 0: > > > if helpmain == "main": > > > else: > > > > What is the else meant to bind to? The innermost if? The 'if function > > == "help"'? It's currently binding to the while. > > > > Recommendation: Break this up! Your main loop is huge! It's way too > > easy to get lost in it. And while you're at it, consider unifying some > > of the similar blocks of code. The solution to both issues is simple: > > Use functions. Have you been taught about them yet? > > > > Also, side tip: Be honest about homework. I'm fairly sure that's what > > this is. :) > > > > ChrisA Its not homework. It is a personal project. |

Re: Else statement executing when it shouldntOn Sunday, January 20, 2013 8:52:12 PM UTC-8, Chris Angelico wrote:
> On Mon, Jan 21, 2013 at 3:40 PM, eli m <techgeek201@gmail.com> wrote: > > > an else statement is running when it shouldnt be. It is on the last line. Whenever i am in the math or game function, when i type in main, it goes back to the start of the program, but it also says not a valid function. I am stumped! > > > > Check your indentation levels. I see a few things here that look odd: > > > > > if function == "help": > > > while helpfunc == 0: > > > if helpmain == "main": > > > else: > > > > What is the else meant to bind to? The innermost if? The 'if function > > == "help"'? It's currently binding to the while. > > > > Recommendation: Break this up! Your main loop is huge! It's way too > > easy to get lost in it. And while you're at it, consider unifying some > > of the similar blocks of code. The solution to both issues is simple: > > Use functions. Have you been taught about them yet? > > > > Also, side tip: Be honest about homework. I'm fairly sure that's what > > this is. :) > > > > ChrisA Its not homework. It is a personal project. |

Re: Else statement executing when it shouldntOn 01/20/2013 11:40 PM, eli m wrote:
> an else statement is running when it shouldnt be. It is on the last line. Whenever i am in the math or game function, when i type in main, it goes back to the start of the program, but it also says not a valid function. I am stumped! > Here is my code: > #Cmd > #Created By Eli M. > #import modules > import random > import math > gtn = 0 > print ("Type in help for a list of cmd functions") > #initiate main loop > cmd = 0 > while cmd == 0: > #ask for input on function > function = raw_input("Type in a function:") > #start math loop > if function == "math": > run = 0 > while run == 0: > #ask for math operation > type = raw_input("What math operation do you want to use?") > if type == "multiplication": > x = raw_input("Type in your first number:") > y = raw_input("Multiply your first number by:") > try: > ans = int(x) * int(y) > print (ans) > try: > ans = float(x) * float(y) > print (ans) > except ValueError, err: > print ("Not a valid number") > except OverflowError, err: > print ("Number too large") > #division math function > if type == "division": > x = raw_input("Type in your first number:") > y = raw_input("Divide your first number by:") > try: > ans = float(x) / float(y) > print (ans) > except ZeroDivisionError, err: > print ("Can't divide by zero") > except ValueError, err: > print ("Not a valid number") > except OverflowError, err: > print ("Number too large") > #subtraction math function > if type == "subtraction": > x = raw_input("Type in your first number:") > y = raw_input("Subtract your first number by:") > try: > ans = float(x) - float(y) > print (ans) > except ValueError, err: > print ("Not a valid number") > #addition math function > if type == "addition": > x = raw_input("Type in your first number:") > y = raw_input("Add your first number by:") > try: > ans = float(x) + float(y) > print (ans) > except ValueError, err: > try: > ans = int(x) + int(y) > print (ans) > except ValueError, err: > print ("Not a valid number") > except OverflowError, err: > print ("Number too large") > #square root math function > if type == "square root": > x = raw_input("Type in your number:") > try: > y = float(x) > z = math.sqrt(y) > print (z) > except ValueError, err: > print ("Not a valid number") > except OverflowError, err: > print ("Number too large") > > #to the power of... math function > if type == "power": > x = raw_input("Type in your number:") > y = raw_input("Multiply your first number by the power of:") > try: > ans = float(x) ** float(y) > print (ans) > except OverflowError, err: > print ("Number too large") > except ValueError, err: > print ("Not a valid number") > #break the math loop > if type == "main": > run = 1 > #absolute value math function > if type == "absolute value": > try: > x = float(raw_input("Type in your number:")) > y = math.fabs(x) > print (y) > except ValueError, err: > print ("Not a valid number") > if function == "random number": > try: > x = int(raw_input("Minimum number:")) > y = int(raw_input("Maximum number:")) > num = random.randint(x, y) > print (num) > except ValueError, err: > print ("Not a valid number") > if function == "games": > games = 0 > while games == 0: > gamechoice = raw_input("What game do you want to play:") > if gamechoice == "guess the number": > run = 0 > while run == 0: > print ("I am thinking of a number between 1 and 20") > num = random.randint(1, 20) > num = int(num) > guesses = 0 > guessestaken = 0 > while guesses == 0: > try: > guess = raw_input("Your guess:") > guess = int(guess) > guessestaken = (guessestaken) + 1 > guessestaken = int(guessestaken) > if guess == (num): > print 'Correct! It took you', int(guessestaken), 'guesses!' > playagain = raw_input("Do you want to play again?") > if playagain == "yes": > guesses = 1 > if playagain == "no": > run = 1 > guesses = 1 > if guess > num: > print ("My number is lower") > if guess < num: > print ("My number is higher") > except TypeError, err: > print ("Not a valid number") > if gamechoice == "main": > games = 1 > > #help function > if function == "help": > helpfunc = 0 > while helpfunc == 0: > #show functions > print ("Functions:") > print ("Math: multiplication, division, subtraction, addition, square root, power, absolute value") > print ("Random Number") > print ("Games: Guess the number") > helpmain = raw_input("Type in main to go back") > if helpmain == "main": > #end helpfunction loop > helpfunc = 1 > cmd = 0 > else: > print ("Not a valid function") Your else is lined up with while, not with if. -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ When a friend succeeds, I die a little. Gore Vidal |

Re: Else statement executing when it shouldntOn Sunday, January 20, 2013 8:54:13 PM UTC-8, René Klačan wrote:
> You have to break while loop not to execute else branch > > > Rene > > > Can you explain in more detail please. |

Re: Else statement executing when it shouldntOn Sunday, January 20, 2013 8:54:13 PM UTC-8, René Klačan wrote:
> You have to break while loop not to execute else branch > > > Rene > > > Can you explain in more detail please. |

Re: Else statement executing when it shouldnt> > > > Your else is lined up with while, not with if. > > > > -m > > > > > > -- > > Lark's Tongue Guide to Python: http://lightbird.net/larks/ > > > > When a friend succeeds, I die a little. Gore Vidal Its lined up. It got messed up when i copied the code into the post. |

All times are GMT. The time now is 11:56 AM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.