Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37138 > unrolled thread
| Started by | eli m <techgeek201@gmail.com> |
|---|---|
| First post | 2013-01-20 20:40 -0800 |
| Last post | 2013-01-22 05:44 +1100 |
| Articles | 20 on this page of 35 — 13 participants |
Back to article view | Back to comp.lang.python
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
Page 1 of 2 [1] 2 Next page →
| From | eli m <techgeek201@gmail.com> |
|---|---|
| Date | 2013-01-20 20:40 -0800 |
| Subject | Else statement executing when it shouldnt |
| Message-ID | <2cc6791f-ba56-406c-a5b0-b23023caf4bb@googlegroups.com> |
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")
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-01-20 23:47 -0500 |
| Message-ID | <roy-CA7811.23472520012013@news.panix.com> |
| In reply to | #37138 |
In article <2cc6791f-ba56-406c-a5b0-b23023caf4bb@googlegroups.com>, 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: [many lines of code elided] TL;DNR :-) A basic debugging technique is to try to find the minimum amount of code that can reproduce the problem. Find some hunk of lines that you're pretty sure can't be at fault, and delete them. See if you can still reproduce the problem. Assuming you can, delete another hunk of code. Keep going until you're down to the smallest possible amount of code which demonstrates the problem. There's a couple of good things that come out of this. One is that it's likely that in the course of doing this, you'll figure out what's wrong. The other is that if you can't figure it out, at least now you'll have something that's easy to show somebody else when you ask for help.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-21 15:52 +1100 |
| Message-ID | <mailman.718.1358743935.2939.python-list@python.org> |
| In reply to | #37138 |
On Mon, Jan 21, 2013 at 3:40 PM, 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! 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
[toc] | [prev] | [next] | [standalone]
| From | eli m <techgeek201@gmail.com> |
|---|---|
| Date | 2013-01-20 20:55 -0800 |
| Message-ID | <22041867-b67f-433e-8c24-451d328d7086@googlegroups.com> |
| In reply to | #37141 |
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 <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! > > > > 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.
[toc] | [prev] | [next] | [standalone]
| From | eli m <techgeek201@gmail.com> |
|---|---|
| Date | 2013-01-20 20:55 -0800 |
| Message-ID | <mailman.723.1358744754.2939.python-list@python.org> |
| In reply to | #37141 |
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 <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! > > > > 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.
[toc] | [prev] | [next] | [standalone]
| From | eli m <techgeek201@gmail.com> |
|---|---|
| Date | 2013-01-20 20:54 -0800 |
| Message-ID | <9f1f33ac-9550-4f39-9c61-7129e0c4f1a9@googlegroups.com> |
| In reply to | #37138 |
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:
>
> 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")
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-01-20 22:00 -0800 |
| Message-ID | <d92d3a6c-e4ab-4dec-935f-86c82d0bd9d7@qi8g2000pbb.googlegroups.com> |
| In reply to | #37142 |
On Jan 21, 2:54 pm, eli m <techgeek...@gmail.com> wrote: > hint: Use the comments in the code to find out where my error is. Pro-tip: when people you're asking for help tell you how you can make it easier for them to help you, a snide response isn't the correct approach.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-21 08:04 +0000 |
| Message-ID | <50fcf69c$0$11101$c3e8da3@news.astraweb.com> |
| In reply to | #37156 |
On Sun, 20 Jan 2013 22:00:10 -0800, alex23 wrote: > On Jan 21, 2:54 pm, eli m <techgeek...@gmail.com> wrote: >> hint: Use the comments in the code to find out where my error is. > > Pro-tip: when people you're asking for help tell you how you can make it > easier for them to help you, a snide response isn't the correct > approach. Alex, thank you for saying this. I can now delete my *much* less polite version saying the same thing. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | René Klačan <rene.klacan@gmail.com> |
|---|---|
| Date | 2013-01-21 05:54 +0100 |
| Message-ID | <mailman.719.1358744056.2939.python-list@python.org> |
| In reply to | #37138 |
[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
>
[toc] | [prev] | [next] | [standalone]
| From | eli m <techgeek201@gmail.com> |
|---|---|
| Date | 2013-01-20 20:57 -0800 |
| Message-ID | <c0d99c75-c82c-4456-a802-55f9bfd43240@googlegroups.com> |
| In reply to | #37143 |
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.
[toc] | [prev] | [next] | [standalone]
| From | René Klačan <rene.klacan@gmail.com> |
|---|---|
| Date | 2013-01-21 06:06 +0100 |
| Message-ID | <mailman.724.1358744774.2939.python-list@python.org> |
| In reply to | #37146 |
[Multipart message — attachments visible in raw view] — view raw
Examples:
# else branch will be executed
i = 0
while i < 5:
i += 1
else:
print('loop is over')
# else branch will be executed
i = 0
while i < 5:
i += 1
if i == 7:
print('i == 7')
break
else:
print('loop is over')
# else branch wont be executed
i = 0
while i < 5:
i += 1
if i == 3:
print('i == 3')
break
else:
print('loop is over')
On Mon, Jan 21, 2013 at 5:57 AM, eli m <techgeek201@gmail.com> wrote:
> 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
>
[toc] | [prev] | [next] | [standalone]
| From | René Klačan <rene.klacan@gmail.com> |
|---|---|
| Date | 2013-01-21 06:07 +0100 |
| Message-ID | <mailman.725.1358744831.2939.python-list@python.org> |
| In reply to | #37146 |
[Multipart message — attachments visible in raw view] — view raw
Examples:
# else branch will be executed
i = 0
while i < 5:
i += 1
else:
print('loop is over')
# else branch will be executed
i = 0
while i < 5:
i += 1
if i == 7:
print('i == 7')
break
else:
print('loop is over')
# else branch wont be executed
i = 0
while i < 5:
i += 1
if i == 3:
print('i == 3')
break
else:
print('loop is over')
On Mon, Jan 21, 2013 at 5:57 AM, eli m <techgeek201@gmail.com> wrote:
> 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
>
[toc] | [prev] | [next] | [standalone]
| From | Thomas Boell <tboell@domain.invalid> |
|---|---|
| Date | 2013-01-22 16:39 +0100 |
| Message-ID | <20130122163924.74038876@sampi> |
| In reply to | #37152 |
On Mon, 21 Jan 2013 06:07:08 +0100
René Klačan <rene.klacan@gmail.com> wrote:
> Examples:
>
> # else branch will be executed
> i = 0
> while i < 5:
> i += 1
> else:
> print('loop is over')
>
>
> # else branch will be executed
> i = 0
> while i < 5:
> i += 1
> if i == 7:
> print('i == 7')
> break
> else:
> print('loop is over')
>
>
> # else branch wont be executed
> i = 0
> while i < 5:
> i += 1
> if i == 3:
> print('i == 3')
> break
> else:
> print('loop is over')
Huh?! I would have expected all your examples to raise a SyntaxError or
IndentationError. Why don't they? Is 'else' not required to have a
matching 'if'?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-23 02:42 +1100 |
| Message-ID | <mailman.799.1358869356.2939.python-list@python.org> |
| In reply to | #37287 |
On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell <tboell@domain.invalid> wrote: > Huh?! I would have expected all your examples to raise a SyntaxError or > IndentationError. Why don't they? Is 'else' not required to have a > matching 'if'? Other things can have else, including 'for' and 'while' loops. :) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Thomas Boell <tboell@domain.invalid> |
|---|---|
| Date | 2013-01-22 16:48 +0100 |
| Message-ID | <20130122164835.74a0ebf6@sampi> |
| In reply to | #37288 |
On Wed, 23 Jan 2013 02:42:27 +1100 Chris Angelico <rosuav@gmail.com> wrote: > On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell <tboell@domain.invalid> wrote: > > Huh?! I would have expected all your examples to raise a SyntaxError or > > IndentationError. Why don't they? Is 'else' not required to have a > > matching 'if'? > > Other things can have else, including 'for' and 'while' loops. :) I must say, that's bound to be confusing for anyone who knows any language other than Python (or none, even). Syntax like that is "an accident waiting to happen"...
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-23 03:07 +1100 |
| Message-ID | <mailman.802.1358870851.2939.python-list@python.org> |
| In reply to | #37291 |
On Wed, Jan 23, 2013 at 2:48 AM, Thomas Boell <tboell@domain.invalid> wrote: > On Wed, 23 Jan 2013 02:42:27 +1100 > Chris Angelico <rosuav@gmail.com> wrote: > >> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell <tboell@domain.invalid> wrote: >> > Huh?! I would have expected all your examples to raise a SyntaxError or >> > IndentationError. Why don't they? Is 'else' not required to have a >> > matching 'if'? >> >> Other things can have else, including 'for' and 'while' loops. :) > > I must say, that's bound to be confusing for anyone who knows any > language other than Python (or none, even). Syntax like that is "an > accident waiting to happen"... It can be confusing and does take that bit of learning, but it's SO handy. In C code, I sometimes simulate it with a 'goto' and a comment, but the actual keyword is much clearer. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-22 23:22 +0000 |
| Message-ID | <50ff1f4c$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #37291 |
On Tue, 22 Jan 2013 16:48:35 +0100, Thomas Boell wrote:
> On Wed, 23 Jan 2013 02:42:27 +1100
> Chris Angelico <rosuav@gmail.com> wrote:
>
>> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell <tboell@domain.invalid>
>> wrote:
>> > Huh?! I would have expected all your examples to raise a SyntaxError
>> > or IndentationError. Why don't they? Is 'else' not required to have a
>> > matching 'if'?
>>
>> Other things can have else, including 'for' and 'while' loops. :)
>
> I must say, that's bound to be confusing for anyone who knows any
> language other than Python (or none, even). Syntax like that is "an
> accident waiting to happen"...
Oh it's even worse than that. I reckon that nearly everyone thinks that
for...else runs the "else" clause if the for loop is empty, at least the
first time they see it. Or for the slow of thinking like me, the first
few dozen times.
# this is wrong, but it looks right
for x in sequence:
do_something_with(x)
else:
print "sequence is empty"
But no, that's not what it does. `for...else` is actually more like this:
# this is right
for x in sequence:
do_something_with(x)
if condition:
break
else:
print "condition was never true"
That's right. The `else` block *unconditionally* executes after the `for`
block. The only way to skip it is to use `break`, which skips all the way
out of the combined for...else statement.
This is a very useful feature, very badly named.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | René Klačan <rene.klacan@gmail.com> |
|---|---|
| Date | 2013-01-23 01:34 +0100 |
| Message-ID | <mailman.848.1358901245.2939.python-list@python.org> |
| In reply to | #37366 |
[Multipart message — attachments visible in raw view] — view raw
it seems that lot of you are forgeting about this case:
for i in [1,2,3,4,5]:
print i
else:
print('this will be printed also because cycle wasnt broke')
so the one case when else branch is executed is when condition is not
satisfied and the other case is when there is no break executed during the
cycle
On Wed, Jan 23, 2013 at 12:22 AM, Steven D'Aprano <
steve+comp.lang.python@pearwood.info> wrote:
> On Tue, 22 Jan 2013 16:48:35 +0100, Thomas Boell wrote:
>
> > On Wed, 23 Jan 2013 02:42:27 +1100
> > Chris Angelico <rosuav@gmail.com> wrote:
> >
> >> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell <tboell@domain.invalid>
> >> wrote:
> >> > Huh?! I would have expected all your examples to raise a SyntaxError
> >> > or IndentationError. Why don't they? Is 'else' not required to have a
> >> > matching 'if'?
> >>
> >> Other things can have else, including 'for' and 'while' loops. :)
> >
> > I must say, that's bound to be confusing for anyone who knows any
> > language other than Python (or none, even). Syntax like that is "an
> > accident waiting to happen"...
>
> Oh it's even worse than that. I reckon that nearly everyone thinks that
> for...else runs the "else" clause if the for loop is empty, at least the
> first time they see it. Or for the slow of thinking like me, the first
> few dozen times.
>
> # this is wrong, but it looks right
> for x in sequence:
> do_something_with(x)
> else:
> print "sequence is empty"
>
>
> But no, that's not what it does. `for...else` is actually more like this:
>
>
> # this is right
> for x in sequence:
> do_something_with(x)
> if condition:
> break
> else:
> print "condition was never true"
>
>
> That's right. The `else` block *unconditionally* executes after the `for`
> block. The only way to skip it is to use `break`, which skips all the way
> out of the combined for...else statement.
>
> This is a very useful feature, very badly named.
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-01-22 22:11 -0500 |
| Message-ID | <mailman.855.1358910678.2939.python-list@python.org> |
| In reply to | #37366 |
On Wed, 23 Jan 2013 01:34:02 +0100, René Kla?an <rene.klacan@gmail.com>
declaimed the following in gmane.comp.python.general:
> it seems that lot of you are forgeting about this case:
>
> for i in [1,2,3,4,5]:
> print i
> else:
> print('this will be printed also because cycle wasnt broke')
>
> so the one case when else branch is executed is when condition is not
> satisfied and the other case is when there is no break executed during the
> cycle
>
The short summary is that the "else:" is performed ANY TIME the loop
exits normally... Only an internal "break" will skip the "else:".
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-01-22 17:28 -0800 |
| Message-ID | <66a33143-c4e5-400d-b6eb-2305a345ca3e@ro7g2000pbb.googlegroups.com> |
| In reply to | #37291 |
On Jan 23, 1:48 am, Thomas Boell <tbo...@domain.invalid> wrote: > I must say, that's bound to be confusing for anyone who knows any > language other than Python (or none, even). Syntax like that is "an > accident waiting to happen"... No, ignorantly expecting every language to conform to every other is the pending accident.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web