Else statement executing when it shouldnt

René Klačan rene.klacan at gmail.com
Sun Jan 20 23:54:13 EST 2013


You have to break while loop not to execute else branch

Rene

On Mon, Jan 21, 2013 at 5:40 AM, eli m <techgeek201 at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130121/b06ba517/attachment.html>


More information about the Python-list mailing list