Else statement executing when it shouldnt
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
Re: Else statement executing when it shouldnt
On 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. -- http://mail.python.org/mailman/listinfo/python-list
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Else statement executing when it shouldnt
On 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: > &
Re: Else statement executing when it shouldnt
On Sunday, January 20, 2013 8:52:12 PM UTC-8, Chris Angelico wrote: > On Mon, Jan 21, 2013 at 3: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! > > > > 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Else statement executing when it shouldnt
On Sunday, January 20, 2013 9:56:59 PM UTC-8, alex23 wrote: > On Jan 21, 2: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 your code with the irrelevancy stripped away: > > > > function = raw_input("Type in a function:") > > #start math loop > > if function == "math": > > #math code > > if function == "random number": > > #random code > > if function == "games": > > #games code > > if function == "help": > > #help code > > else: > > print ("Not a valid function") > > > > Say you enter 'math'. It passes the first condition, so runs the math > > code. > > It then fails on the next 3 conditions, the last of which has an else, > > so if you type _anything_ other than 'help', you'll see "Not a valid > > function". > > > > Easy answer, use `elif` ("else if") instead of else for the subsequent > > tests: > > > > if function == "math": > > #math code > > elif function == "random number": > > #random code > > elif function == "games": > > #games code > > elif function == "help": > > #help code > > else: > > print ("Not a valid function") > > > > Better answer: read up on real functions, and look into dictionary > > dispatch: > > > > def f_math(): > >#math code > > > > def f_random_number(): > >#random code > > > > > > > > function_dispatcher = { > > 'math': f_math, > > 'random number': f_random_number, > > > > } > > > >while cmd == 0: > >function_name = raw_input("Type in a function:") > >if function_name in function_dispatcher: > >function_dispatcher[function_name]() > >else: > >print("Not a valid function") > > > > To have your functions break out of the loop, use a `global` variable > > or pass a context object into each function to allow them to set > > `cmd`. Thank you, that solved my problem. Sorry for my posts, i am a noob and this is my first time posting on here. -- http://mail.python.org/mailman/listinfo/python-list
Pick random choice from variables
How do i make something with python that will ask the user for input, and then use the random.choice function to select a random choice from what the user entered. -- http://mail.python.org/mailman/listinfo/python-list
How would you do this?
I want to make a guess the number game (Which i have), but i want to make the computer play the game against itself. How would i do this? -- http://mail.python.org/mailman/listinfo/python-list
Re: How would you do this?
On Thursday, February 14, 2013 4:09:37 PM UTC-8, Oscar Benjamin wrote: > On 14 February 2013 23:34, eli m wrote: > > > I want to make a guess the number game (Which i have), but i want to make > > the computer play the game against itself. How would i do this? > > > > Your question would make more sense if you would show your program and > > also explain how you would like the output to look when the computer > > played itself. > > > > > > Oscar This is my code: #Guess the number game import random run = 0 while run == 0: print ("I am thinking of a number between 1 and 100") num = random.randint(1, 100) 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 if guess > num: print ("My number is lower") if guess < num: print ("My number is higher") except TypeError, err: print ("Not a valid number") I would like it to show the computer guessing the numbers. -- http://mail.python.org/mailman/listinfo/python-list
Small program ideas
Any small program ideas? I would prefer to stick to command line ones. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Small program ideas
On Friday, February 15, 2013 7:52:57 PM UTC-8, Mitya Sirenef wrote: > On 02/15/2013 10:22 PM, eli m wrote: > > > Any small program ideas? I would prefer to stick to command line ones. > > Thanks. > > > > How about these two: > > > > - simulation of a street crossing with green/red lights allowing cars > > and pedestrians to pass in one direction then another > > > > - simulation of an elevator in a building: buttons on each floor to > > call the elevator, buttons inside to go to a particular floor, > > multiple floors can be selected at the same time, creating a queue > > of floors to go to. > > > > -m > > > > -- > > Lark's Tongue Guide to Python: http://lightbird.net/larks/ Could i make these text and not visual? That is what i am trying to do. -- http://mail.python.org/mailman/listinfo/python-list
Import Question
How long does it take for the program to import something? I am asking this because i have like 7 imports at the beginning of my program and i am thinking thats the reason why it is slow to start up. Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Small program ideas
On Friday, February 15, 2013 7:22:41 PM UTC-8, eli m wrote: > Any small program ideas? I would prefer to stick to command line ones. Thanks. Thank you guys for the suggestions. Any more? -- http://mail.python.org/mailman/listinfo/python-list
Re: Small program ideas
On Monday, February 25, 2013 10:15:24 PM UTC-8, Dave Angel wrote: > On 02/25/2013 10:48 PM, eli m wrote: > > > On Friday, February 15, 2013 7:22:41 PM UTC-8, eli m wrote: > > >> Any small program ideas? I would prefer to stick to command line ones. > >> Thanks. > > > > > > Thank you guys for the suggestions. Any more? > > > > > > > There are all kinds of things you could do. First, consider something > > that might be useful. > > > > 1) checksum all the files in a directory tree, using various checksum > > algorithms. > > > > 2) Convert one kind of file to another. > > > > 3) Calculate time between two dates > > > > 4) Write some part of a backup system. For example, copy files from a > > directory tree into a specified directory, stopping when the size totals > > N.N gig, and keeping track of which files have been so processed, so > > that after burning that directory to DVD, you can repeat the process. > > As a bonus, add a utility & datafile to the top of that directory, so > > that the DVD can be self-checking. > > > > Then try something interesting: > > > > 1) find the nth prime, for example the 1000th prime > > > > 2) Find all perfect numbers under a trillion > > > > 3) solve the puzzles on http://projecteuler.net > > > > 4) Build a spell checker, using a combination of a standard > > dictionary-list and custom entries. Bonus question - Make it smart > > enough to only spell-check comments and literal strings, when applied to > > files with an extension of .py > > > > > > -- > > DaveA How hard would it be to change one file to another and would it be a small-medium sized program? -- http://mail.python.org/mailman/listinfo/python-list
Re: Small program ideas
On Tuesday, February 26, 2013 4:22:10 PM UTC-8, Joshua Landau wrote: > On 26 February 2013 22:47, eli m wrote: > > > > > > How hard would it be to change one file to another and would it be a > small-medium sized program? > > > How do you want to change it? Like rename a file (os.rename)? I want to change the file type. -- http://mail.python.org/mailman/listinfo/python-list
Display video in tkinter?
Is there a way to display video (an avi) in tkinter without having to download other modules? If not then are there any other options? -- http://mail.python.org/mailman/listinfo/python-list
How would you do this?
How would you find the slope, y intercept, and slope-intercept form equation for a line in python? -- http://mail.python.org/mailman/listinfo/python-list
Store a variable permanently
So i have a variable called funds that i want to store the value of even after the program is exited. My funds variable holds the total value of funds i have. I add a certain number of funds each time i run the program by entering how much i want to add. How would i store the funds variable to keep its value? -- http://mail.python.org/mailman/listinfo/python-list
Input wont work with if statement if it has a space
Hi guys, i have a program like this: (A lot of code is not included) run = 0 while run == 0: raw_input("Type in a function:") if function == "Example": print ("Hello World!") else: print ("blah blah blah") The problem is that whenever i type in example with a space after it then it prints the else statement. My question is, how do i get it to except input with spaces? -- http://mail.python.org/mailman/listinfo/python-list
Re: Input wont work with if statement if it has a space
On Tuesday, March 5, 2013 3:31:13 PM UTC-8, eli m wrote: > Hi guys, i have a program like this: (A lot of code is not included) > > run = 0 > > while run == 0: > >raw_input("Type in a function:") > >if function == "Example": > > print ("Hello World!") > >else: > > print ("blah blah blah") > > > > The problem is that whenever i type in example with a space after it then it > prints the else statement. > > > > My question is, how do i get it to except input with spaces? oops i meant function = raw_input("Type in a function:") -- http://mail.python.org/mailman/listinfo/python-list
Re: Input wont work with if statement if it has a space
On Tuesday, March 5, 2013 3:47:31 PM UTC-8, emile wrote: > On 03/05/2013 03:33 PM, eli m wrote: > > > On Tuesday, March 5, 2013 3:31:13 PM UTC-8, eli m wrote: > > >> Hi guys, i have a program like this: (A lot of code is not included) > > >> > > >> run = 0 > > >> > > >> while run == 0: > > >> > > >> function = raw_input("Type in a function:") > > >> > > >> if function == "Example": > > > > whenever function isn't _exactly_ "Example" the else clause executes. > > > > If you want to catch forms of example you might try: > > > > if function.strip().upper() == "EXAMPLE": > > > > Emile > > > > > > > > > > >> > > >>print ("Hello World!") > > >> > > >> else: > > >> > > >>print ("blah blah blah") > > >> > > >> > > >> > > >> The problem is that whenever i type in example with a space after it then > >> it prints the else statement. > > >> > > >> > > >> > > >> My question is, how do i get it to except input with spaces? > > > > > > oops i meant function = raw_input("Type in a function:") > > > Thank you! -- http://mail.python.org/mailman/listinfo/python-list
Insert comma in number?
I have a python program that accepts input and calculates the factorial of that number, and i want to know if i can make it so commas get inserted in the number. For example: instead of 1000 it would say 1,000 -- http://mail.python.org/mailman/listinfo/python-list
Re: Small program ideas
Any other ideas? -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem this random seed()
On Monday, March 18, 2013 6:57:30 PM UTC-7, NZach wrote: > Hello everyone, > > > > i am using the MMK.py example from a SimPy tutorial, which can be found here: > http://heather.cs.ucdavis.edu/~matloff/156/PLN/DESimIntro.pdf > > > > I have made very little changes to the code and i have upload it here: > > http://codeviewer.org/view/code:30d3 > > > > The problem is that i am executing the main() function in lines 83 and 90, > but > > i do not receive the same result, although the random seed is used in the G > > class. > > > > Any idea please, how can i solve it ? > > > > > > Thanks, > > > > Nicholas. What errors did it show? -- http://mail.python.org/mailman/listinfo/python-list
Video tutorial for making a guess the number game in python
I made a video tutorial for making a guess the number game in python. You can check it out here: http://www.youtube.com/watch?v=0WSQb-7wMJQ -- http://mail.python.org/mailman/listinfo/python-list