Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37143

Re: Else statement executing when it shouldnt

References <2cc6791f-ba56-406c-a5b0-b23023caf4bb@googlegroups.com>
Date 2013-01-21 05:54 +0100
Subject Re: Else statement executing when it shouldnt
From René Klačan <rene.klacan@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.719.1358744056.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

You have to break while loop not to execute else branch

Rene

On Mon, Jan 21, 2013 at 5:40 AM, 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:
> #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")
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:40 -0800
  Re: Else statement executing when it shouldnt Roy Smith <roy@panix.com> - 2013-01-20 23:47 -0500
  Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-21 15:52 +1100
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:55 -0800
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:55 -0800
  Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:54 -0800
    Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-20 22:00 -0800
      Re: Else statement executing when it shouldnt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-21 08:04 +0000
  Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-21 05:54 +0100
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:57 -0800
      Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-21 06:06 +0100
      Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-21 06:07 +0100
        Re: Else statement executing when it shouldnt Thomas Boell <tboell@domain.invalid> - 2013-01-22 16:39 +0100
          Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-23 02:42 +1100
            Re: Else statement executing when it shouldnt Thomas Boell <tboell@domain.invalid> - 2013-01-22 16:48 +0100
              Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-23 03:07 +1100
              Re: Else statement executing when it shouldnt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-22 23:22 +0000
                Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-23 01:34 +0100
                Re: Else statement executing when it shouldnt Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-01-22 22:11 -0500
              Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-22 17:28 -0800
                Re: Else statement executing when it shouldnt Thomas Boell <tboell@domain.invalid> - 2013-01-23 12:22 +0100
                Re: Else statement executing when it shouldnt Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-01-23 15:35 +0200
                Re: Else statement executing when it shouldnt Jerry Hill <malaclypse2@gmail.com> - 2013-01-23 09:53 -0500
                Re: Else statement executing when it shouldnt Frank Millman <frank@chagford.com> - 2013-01-25 10:15 +0200
          Re: Else statement executing when it shouldnt Duncan Booth <duncan.booth@invalid.invalid> - 2013-01-22 15:48 +0000
            Re: Else statement executing when it shouldnt Duncan Booth <duncan.booth@invalid.invalid> - 2013-01-22 15:52 +0000
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:57 -0800
  Re: Else statement executing when it shouldnt Mitya Sirenef <msirenef@lightbird.net> - 2013-01-20 23:57 -0500
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:59 -0800
      Re: Else statement executing when it shouldnt Mitya Sirenef <msirenef@lightbird.net> - 2013-01-21 01:09 -0500
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:59 -0800
      Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-20 21:46 -0800
  Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-20 21:56 -0800
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-21 07:37 -0800
      Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-22 05:44 +1100

csiph-web