You have to break while loop not to execute else branch<div><br></div><div>Rene<br><br><div class="gmail_quote">On Mon, Jan 21, 2013 at 5:40 AM, eli m <span dir="ltr"><<a href="mailto:techgeek201@gmail.com" target="_blank">techgeek201@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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!<br>
Here is my code:<br>
#Cmd<br>
#Created By Eli M.<br>
#import modules<br>
import random<br>
import math<br>
gtn = 0<br>
print ("Type in help for a list of cmd functions")<br>
#initiate main loop<br>
cmd = 0<br>
while cmd == 0:<br>
#ask for input on function<br>
function = raw_input("Type in a function:")<br>
#start math loop<br>
if function == "math":<br>
run = 0<br>
while run == 0:<br>
#ask for math operation<br>
type = raw_input("What math operation do you want to use?")<br>
if type == "multiplication":<br>
x = raw_input("Type in your first number:")<br>
y = raw_input("Multiply your first number by:")<br>
try:<br>
ans = int(x) * int(y)<br>
print (ans)<br>
try:<br>
ans = float(x) * float(y)<br>
print (ans)<br>
except ValueError, err:<br>
print ("Not a valid number")<br>
except OverflowError, err:<br>
print ("Number too large")<br>
#division math function<br>
if type == "division":<br>
x = raw_input("Type in your first number:")<br>
y = raw_input("Divide your first number by:")<br>
try:<br>
ans = float(x) / float(y)<br>
print (ans)<br>
except ZeroDivisionError, err:<br>
print ("Can't divide by zero")<br>
except ValueError, err:<br>
print ("Not a valid number")<br>
except OverflowError, err:<br>
print ("Number too large")<br>
#subtraction math function<br>
if type == "subtraction":<br>
x = raw_input("Type in your first number:")<br>
y = raw_input("Subtract your first number by:")<br>
try:<br>
ans = float(x) - float(y)<br>
print (ans)<br>
except ValueError, err:<br>
print ("Not a valid number")<br>
#addition math function<br>
if type == "addition":<br>
x = raw_input("Type in your first number:")<br>
y = raw_input("Add your first number by:")<br>
try:<br>
ans = float(x) + float(y)<br>
print (ans)<br>
except ValueError, err:<br>
try:<br>
ans = int(x) + int(y)<br>
print (ans)<br>
except ValueError, err:<br>
print ("Not a valid number")<br>
except OverflowError, err:<br>
print ("Number too large")<br>
#square root math function<br>
if type == "square root":<br>
x = raw_input("Type in your number:")<br>
try:<br>
y = float(x)<br>
z = math.sqrt(y)<br>
print (z)<br>
except ValueError, err:<br>
print ("Not a valid number")<br>
except OverflowError, err:<br>
print ("Number too large")<br>
<br>
#to the power of... math function<br>
if type == "power":<br>
x = raw_input("Type in your number:")<br>
y = raw_input("Multiply your first number by the power of:")<br>
try:<br>
ans = float(x) ** float(y)<br>
print (ans)<br>
except OverflowError, err:<br>
print ("Number too large")<br>
except ValueError, err:<br>
print ("Not a valid number")<br>
#break the math loop<br>
if type == "main":<br>
run = 1<br>
#absolute value math function<br>
if type == "absolute value":<br>
try:<br>
x = float(raw_input("Type in your number:"))<br>
y = math.fabs(x)<br>
print (y)<br>
except ValueError, err:<br>
print ("Not a valid number")<br>
if function == "random number":<br>
try:<br>
x = int(raw_input("Minimum number:"))<br>
y = int(raw_input("Maximum number:"))<br>
num = random.randint(x, y)<br>
print (num)<br>
except ValueError, err:<br>
print ("Not a valid number")<br>
if function == "games":<br>
games = 0<br>
while games == 0:<br>
gamechoice = raw_input("What game do you want to play:")<br>
if gamechoice == "guess the number":<br>
run = 0<br>
while run == 0:<br>
print ("I am thinking of a number between 1 and 20")<br>
num = random.randint(1, 20)<br>
num = int(num)<br>
guesses = 0<br>
guessestaken = 0<br>
while guesses == 0:<br>
try:<br>
guess = raw_input("Your guess:")<br>
guess = int(guess)<br>
guessestaken = (guessestaken) + 1<br>
guessestaken = int(guessestaken)<br>
if guess == (num):<br>
print 'Correct! It took you', int(guessestaken), 'guesses!'<br>
playagain = raw_input("Do you want to play again?")<br>
if playagain == "yes":<br>
guesses = 1<br>
if playagain == "no":<br>
run = 1<br>
guesses = 1<br>
if guess > num:<br>
print ("My number is lower")<br>
if guess < num:<br>
print ("My number is higher")<br>
except TypeError, err:<br>
print ("Not a valid number")<br>
if gamechoice == "main":<br>
games = 1<br>
<br>
#help function<br>
if function == "help":<br>
helpfunc = 0<br>
while helpfunc == 0:<br>
#show functions<br>
print ("Functions:")<br>
print ("Math: multiplication, division, subtraction, addition, square root, power, absolute value")<br>
print ("Random Number")<br>
print ("Games: Guess the number")<br>
helpmain = raw_input("Type in main to go back")<br>
if helpmain == "main":<br>
#end helpfunction loop<br>
helpfunc = 1<br>
cmd = 0<br>
else:<br>
print ("Not a valid function")<br>
<span class="HOEnZb"><font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div>