Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'root': 0.04; 'function,': 0.07; 'root,': 0.07; 'try:': 0.07; 'cmd': 0.09; 'err:': 0.09; 'python:': 0.09; '"guess': 0.16; '"no":': 0.16; '"yes":': 0.16; '20)': 0.16; 'ans': 0.16; 'division,': 0.16; 'int(x)': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'subject:when': 0.16; 'succeeds,': 0.16; 'valueerror,': 0.16; 'wrote:': 0.17; 'input': 0.18; 'math': 0.20; 'import': 0.21; 'addition,': 0.21; 'absolute': 0.23; 'statement': 0.23; 'random': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'guess': 0.27; 'run': 0.28; 'function': 0.30; 'running': 0.32; 'print': 0.32; 'goes': 0.33; 'says': 0.33; 'function.': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'program,': 0.34; 'list': 0.35; 'pm,': 0.35; 'except': 0.36; 'but': 0.36; 'be.': 0.36; 'modules': 0.36; 'too': 0.36; 'skip:z 10': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; 'first': 0.61; 'back': 0.62; 'between': 0.63; 'here': 0.65; 'power': 0.74; 'square': 0.75; 'friend': 0.81; 'divide': 0.84; 'power,': 0.91; 'eli': 0.93 Date: Sun, 20 Jan 2013 23:57:03 -0500 From: Mitya Sirenef User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Else statement executing when it shouldnt References: <2cc6791f-ba56-406c-a5b0-b23023caf4bb@googlegroups.com> In-Reply-To: <2cc6791f-ba56-406c-a5b0-b23023caf4bb@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 179 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358744227 news.xs4all.nl 6875 [2001:888:2000:d::a6]:34702 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37145 On 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