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


Groups > comp.lang.python > #43167 > unrolled thread

While loop help

Started bythomasancilleri@gmail.com
First post2013-04-09 06:32 -0700
Last post2013-04-09 12:19 -0700
Articles 20 on this page of 30 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  While loop help thomasancilleri@gmail.com - 2013-04-09 06:32 -0700
    Re: While loop help thomasancilleri@gmail.com - 2013-04-09 06:57 -0700
      Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 13:12 -0400
        Re: While loop help thomasancilleri@gmail.com - 2013-04-09 10:18 -0700
          Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 03:23 +1000
          Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 13:30 -0400
    Re: While loop help thomasancilleri@gmail.com - 2013-04-09 06:58 -0700
      Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 00:05 +1000
        Re: While loop help thomasancilleri@gmail.com - 2013-04-09 08:47 -0700
          Re: While loop help Joel Goldstick <joel.goldstick@gmail.com> - 2013-04-09 12:02 -0400
          Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 02:10 +1000
            Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:24 -0700
              Re: While loop help Joel Goldstick <joel.goldstick@gmail.com> - 2013-04-09 12:36 -0400
              Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 02:47 +1000
                Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:57 -0700
                  Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 03:08 +1000
                  Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 13:27 -0400
                Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:57 -0700
            Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:24 -0700
            Re: While loop help Walter Hurry <walterhurry@lavabit.com> - 2013-04-09 19:35 +0000
              Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 16:12 -0400
                Re: While loop help Walter Hurry <walterhurry@lavabit.com> - 2013-04-09 20:59 +0000
                  Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 08:36 +1000
          Re: While loop help rusi <rustompmody@gmail.com> - 2013-04-10 08:59 -0700
        Re: While loop help thomasancilleri@gmail.com - 2013-04-09 08:47 -0700
    Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 00:00 +1000
    Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:49 -0700
      Re: While loop help Larry Hudson <orgnut@yahoo.com> - 2013-04-09 23:44 -0700
        Re: While loop help Larry Hudson <orgnut@yahoo.com> - 2013-04-10 20:00 -0700
    Re: While loop help jmfauth <wxjmfauth@gmail.com> - 2013-04-09 12:19 -0700

Page 1 of 2  [1] 2  Next page →


#43167 — While loop help

Fromthomasancilleri@gmail.com
Date2013-04-09 06:32 -0700
SubjectWhile loop help
Message-ID<b93147e7-9d17-4232-99a3-767b88f9e9ba@googlegroups.com>
I'm new to learning python and creating a basic program to convert units of measurement which I will eventually expand upon but im trying to figure out how to loop the entire program. When I insert a while loop it only loops the first 2 lines. Can someone provide a detailed beginner friendly explanation. Here is my program.

#!/usr/bin/env python
restart = "true"
while restart == "true":
#Program starts here
    print "To start the Unit Converter please type the number next to the conversion you would like to perform"
    choice = input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres to Square-Miles\n")

#If user enters 1:Program converts inches to meters
    if choice == 1:
        number = int(raw_input("\n\nType the amount in Inches you would like to convert to Meters.\n"))
        operation = "Inches to Meters"
        calc = round(number * .0254, 2)
        print "\n",number,"Inches =",calc,"Meters"
        restart = raw_input("If you would like to perform another conversion type: true\n"

#If user enters 2:Program converts millimeters to pints  
    elif choice == 2:
        number = int(raw_input("\n\nType the amount in Milliliters you would like to convert to Pints.\n"))
        operation = "Milliliters to Pints"
        calc = round(number * 0.0021134,2)
        print "\n",number,"Milliliters =",calc,"Pints"
        restart = raw_input("If you would like to perform another conversion type: true\n")

#If user enter 3:Program converts kilometers to miles
    elif choice == 3:
        number = int(raw_input("\n\nType the amount in Kilometers you would like to convert to Miles.\n"))
        operation = "Kilometers to Miles"
        calc = round(number * 0.62137,2)
        print "\n",number,"Kilometers =",calc,"Miles"
        restart = raw_input("If you would like to perform another conversion type: true\n")

[toc] | [next] | [standalone]


#43169

Fromthomasancilleri@gmail.com
Date2013-04-09 06:57 -0700
Message-ID<cbd51f16-98d4-490e-92cf-7bbec3636401@googlegroups.com>
In reply to#43167
On Tuesday, April 9, 2013 9:32:18 AM UTC-4, thomasa...@gmail.com wrote:
> I'm new to learning python and creating a basic program to convert units of measurement which I will eventually expand upon but im trying to figure out how to loop the entire program. When I insert a while loop it only loops the first 2 lines. Can someone provide a detailed beginner friendly explanation. Here is my program. Also suddenly I'm getting an invalid syntax error next to my elif statements when I wasn't a minute ago. What is wrong here?
> 
> 
> 
> #!/usr/bin/env python
> 
> restart = "true"
> 
> while restart == "true":
> 
> #Program starts here
> 
>     print "To start the Unit Converter please type the number next to the conversion you would like to perform"
> 
>     choice = input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres to Square-Miles\n")
> 
> 
> 
> #If user enters 1:Program converts inches to meters
> 
>     if choice == 1:
> 
>         number = int(raw_input("\n\nType the amount in Inches you would like to convert to Meters.\n"))
> 
>         operation = "Inches to Meters"
> 
>         calc = round(number * .0254, 2)
> 
>         print "\n",number,"Inches =",calc,"Meters"
> 
>         restart = raw_input("If you would like to perform another conversion type: true\n"
> 
> 
> 
> #If user enters 2:Program converts millimeters to pints  
> 
>     elif choice == 2:
> 
>         number = int(raw_input("\n\nType the amount in Milliliters you would like to convert to Pints.\n"))
> 
>         operation = "Milliliters to Pints"
> 
>         calc = round(number * 0.0021134,2)
> 
>         print "\n",number,"Milliliters =",calc,"Pints"
> 
>         restart = raw_input("If you would like to perform another conversion type: true\n")
> 
> 
> 
> #If user enter 3:Program converts kilometers to miles
> 
>     elif choice == 3:
> 
>         number = int(raw_input("\n\nType the amount in Kilometers you would like to convert to Miles.\n"))
> 
>         operation = "Kilometers to Miles"
> 
>         calc = round(number * 0.62137,2)
> 
>         print "\n",number,"Kilometers =",calc,"Miles"
> 
>         restart = raw_input("If you would like to perform another conversion type: true\n")

[toc] | [prev] | [next] | [standalone]


#43204

FromDave Angel <davea@davea.name>
Date2013-04-09 13:12 -0400
Message-ID<mailman.369.1365527582.3114.python-list@python.org>
In reply to#43169
On 04/09/2013 09:57 AM, thomasancilleri@gmail.com wrote:
> On Tuesday, April 9, 2013 9:32:18 AM UTC-4, thomasa...@gmail.com wrote:
>> I'm new to learning python and creating a basic program to convert units of measurement which I will eventually expand upon but im trying to figure out how to loop the entire program. When I insert a while loop it only loops the first 2 lines. Can someone provide a detailed beginner friendly explanation. Here is my program. Also suddenly I'm getting an invalid syntax error next to my elif statements when I wasn't a minute ago. What is wrong here?
>>
>>
>>
>> #!/usr/bin/env python
>>
>> restart = "true"
>>
>> while restart == "true":
>>
>> #Program starts here
>>
>>      print "To start the Unit Converter please type the number next to the conversion you would like to perform"
>>
>>      choice = input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres to Square-Miles\n")
>>
Quoting that many lines, and double-spacing every one of them is very 
impolite.  Almost makes me want to skip the thread.

If you must use buggy googlegroups, then at least fix its most annoying 
bugs:

     read this http://wiki.python.org/moin/GoogleGroupsPython.

(rest deleted, as the point has been made)



-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#43205

Fromthomasancilleri@gmail.com
Date2013-04-09 10:18 -0700
Message-ID<a530ef2b-f32f-4413-9c9d-4c126991baf4@googlegroups.com>
In reply to#43204
sorry i just started using google groups, not really all that sure how to use it properly.

[toc] | [prev] | [next] | [standalone]


#43206

FromChris Angelico <rosuav@gmail.com>
Date2013-04-10 03:23 +1000
Message-ID<mailman.370.1365528216.3114.python-list@python.org>
In reply to#43205
On Wed, Apr 10, 2013 at 3:18 AM,  <thomasancilleri@gmail.com> wrote:
> sorry i just started using google groups, not really all that sure how to use it properly.

The best way to use Google Groups is to not use it, frankly. Just
subscribe to the mailing list in gmail; it has its own issues (eg it
encourages top-posting by putting a couple of blank lines at the top
of the quoted text), but they're easier to work around than the GG
problems.

ChrisA

[toc] | [prev] | [next] | [standalone]


#43208

FromDave Angel <davea@davea.name>
Date2013-04-09 13:30 -0400
Message-ID<mailman.372.1365528663.3114.python-list@python.org>
In reply to#43205
On 04/09/2013 01:18 PM, thomasancilleri@gmail.com wrote:
> sorry i just started using google groups, not really all that sure how to use it properly.
>

IMHO, best way is to switch to a good email program, and mail your 
messages to comp.lang.python.  That's after subscribing via
     http://mail.python.org/mailman/listinfo/python-list

You can set up filters in your email program so all forum messages to 
into a separate folder, one per forum.  And you tell your email program 
to use text mail, not html.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#43170

Fromthomasancilleri@gmail.com
Date2013-04-09 06:58 -0700
Message-ID<596ca4b8-b5aa-4112-b086-6320108075f7@googlegroups.com>
In reply to#43167
Also I'm getting a invalid syntax next to my elif statements that i wasnt getting before. why is this happening now?

[toc] | [prev] | [next] | [standalone]


#43173

FromChris Angelico <rosuav@gmail.com>
Date2013-04-10 00:05 +1000
Message-ID<mailman.348.1365516328.3114.python-list@python.org>
In reply to#43170
On Tue, Apr 9, 2013 at 11:58 PM,  <thomasancilleri@gmail.com> wrote:
> Also I'm getting a invalid syntax next to my elif statements that i wasnt getting before. why is this happening now?

Ah! That's relating to the close parenthesis problem I mentioned.
That's the exact issue I saw.

When you get told about a problem, sometimes the location pointed out
isn't the actual cause. What you have is a (near) guarantee that the
problem is no later in the file than that point; often it'll be on
that line or the one previous line. In this case, the code is
"raw_input(........ elif", which can't be properly parsed - the open
parenthesis is forcing the code to be interpreted as an expression,
and elif isn't an expression.

If you're stuck figuring out a problem, one neat trick is to delete
the line of code that's blamed for the problem and try again. If the
problem disappears, it was on that line; if the problem moves down to
the next line, it's probably on the preceding line. This trick doesn't
always work, but it can occasionally be quite handy.

ChrisA

[toc] | [prev] | [next] | [standalone]


#43187

Fromthomasancilleri@gmail.com
Date2013-04-09 08:47 -0700
Message-ID<a530b25f-f9d9-45e9-925c-0ecd8a660cf6@googlegroups.com>
In reply to#43173
Sorry I'm just starting to learn python and I'm not sure what version I'm using to be quite honest. I just started typing it up in Komodo edit (Which I'm not personally a fan in particular) I fixed the missing parenthesis which fixed the invalid syntax problem. Also, I apologize for submitting code that did not run. This is the code I have now before I tried looping it again:

#!/usr/bin/env python

#Program starts here
print "To start the Unit Converter please type the number next to the conversion you would like to perform"
choice = raw_input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres to Square-Miles\n")

#If user enters 1:Program converts inches to meters
if choice == 1:
    #operation = "Inches to Meters"
    number = int(raw_input("\n\nType the amount in Inches you would like to convert to Meters.\n"))
    calc = round(number * .0254, 2)
    print "\n",number,"Inches =",calc,"Meters"
    restart = raw_input("If you would like to perform another conversion type: true\n")
    
#If user enters 2:Program converts millimeters to pints
elif choice == 2:
    #operation = "Milliliters to Pints"    
    number = int(raw_input("\n\nType the amount in Milliliters you would like to convert to Pints.\n"))
    calc = round(number * 0.0021134,2)
    print "\n",number,"Milliliters =",calc,"Pints"
    restart = raw_input("If you would like to perform another conversion type: true\n")
    
#If user enter 3:Program converts kilometers to miles
elif choice == 3:
    #operation = "Kilometers to Miles"
    number = int(raw_input("\n\nType the amount in Kilometers you would like to convert to Miles.\n"))
    calc = round(number * 0.62137,2)
    print "\n",number,"Kilometers =",calc,"Miles"
    restart = raw_input("If you would like to perform another conversion type: true\n")

Not sure what you meant to exactly by this:
"There's a lot of duplicated code here, most notably your continuation 
condition. You can simply back-tab after the elif block and have some 
code that reunites all the branches; this would also make things 
clearer"

Thanks for your reply and if you have any ideas for me to improve my coding that will prevent me from learning python in a sloppy way. I'd like to learn it correctly the first time!

[toc] | [prev] | [next] | [standalone]


#43189

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-04-09 12:02 -0400
Message-ID<mailman.357.1365523346.3114.python-list@python.org>
In reply to#43187

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

On Tue, Apr 9, 2013 at 11:47 AM, <thomasancilleri@gmail.com> wrote:

> Sorry I'm just starting to learn python and I'm not sure what version I'm
> using to be quite honest. I just started typing it up in Komodo edit (Which
> I'm not personally a fan in particular) I fixed the missing parenthesis
> which fixed the invalid syntax problem. Also, I apologize for submitting
> code that did not run. This is the code I have now before I tried looping
> it again:
>
> #!/usr/bin/env python
>
> #Program starts here
> print "To start the Unit Converter please type the number next to the
> conversion you would like to perform"
>

choice is not converted to an integer.  raw_input returns a string.


> choice = raw_input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres to
> Square-Miles\n")
>

 This will convert choice to an int.  Actually, it might not if what you
type is not a number.  Then it will cause and exception.
But for now, do this:

> choice = int(choice)
>

or leave choices as a string and make your if statements like if choice ==
'1'


> #If user enters 1:Program converts inches to meters
> if choice == 1:
>     #operation = "Inches to Meters"
>     number = int(raw_input("\n\nType the amount in Inches you would like
> to convert to Meters.\n"))
>     calc = round(number * .0254, 2)
>     print "\n",number,"Inches =",calc,"Meters"
>     restart = raw_input("If you would like to perform another conversion
> type: true\n")
>
> #If user enters 2:Program converts millimeters to pints
> elif choice == 2:
>     #operation = "Milliliters to Pints"
>     number = int(raw_input("\n\nType the amount in Milliliters you would
> like to convert to Pints.\n"))
>     calc = round(number * 0.0021134,2)
>     print "\n",number,"Milliliters =",calc,"Pints"
>     restart = raw_input("If you would like to perform another conversion
> type: true\n")
>
> #If user enter 3:Program converts kilometers to miles
> elif choice == 3:
>     #operation = "Kilometers to Miles"
>     number = int(raw_input("\n\nType the amount in Kilometers you would
> like to convert to Miles.\n"))
>     calc = round(number * 0.62137,2)
>     print "\n",number,"Kilometers =",calc,"Miles"
>     restart = raw_input("If you would like to perform another conversion
> type: true\n")
>
> Not sure what you meant to exactly by this:
> "There's a lot of duplicated code here, most notably your continuation
> condition. You can simply back-tab after the elif block and have some
> code that reunites all the branches; this would also make things
> clearer"
>
> Thanks for your reply and if you have any ideas for me to improve my
> coding that will prevent me from learning python in a sloppy way. I'd like
> to learn it correctly the first time!
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#43191

FromChris Angelico <rosuav@gmail.com>
Date2013-04-10 02:10 +1000
Message-ID<mailman.359.1365523839.3114.python-list@python.org>
In reply to#43187
On Wed, Apr 10, 2013 at 1:47 AM,  <thomasancilleri@gmail.com> wrote:
> ... I'm not sure what version I'm using ...

Try putting these lines into a Python script:

import sys
print(sys.version)

That, on any version of Python (back a fairly long way, Steven
D'Aprano can probably say how far), will give you a line or so of
output that summarizes your version. For instance, I can get the
following:

3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)]

2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]

3.1.1+ (r311:74480, Nov  2 2009, 14:49:22)
[GCC 4.4.1]

3.4.0a0 (default:5dcd7ee0716a, Mar 30 2013, 08:17:06)
[GCC 4.7.2]

It's a handy system summary.

> choice = raw_input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres to Square-Miles\n")
> if choice == 1:

You probably want to use int(raw_input(...)) here; currently, you're
going to get back a string eg "1", which is not equal to the integer
1. But at least now you don't have the automatic evaluation happening
:)

>     restart = raw_input("If you would like to perform another conversion type: true\n")
>
> Not sure what you meant to exactly by this:
> "There's a lot of duplicated code here, most notably your continuation
> condition. You can simply back-tab after the elif block and have some
> code that reunites all the branches; this would also make things
> clearer"

Notice how you have the "restart = " line (which I quote above)
duplicated into each of your code branches? That's what I'm talking
about. Here's a pseudocode version of the looping you have:

while restart:
  choice = get_choice()
  if choice == 1:
    do_choice_1()
    restart = get_restart()
  elif choice == 2:
    do_choice_2()
    restart = get_restart()
  elif choice == 3:
    do_choice_3()
    restart = get_restart()

Here's how you could deduplicate that:

while restart:
  choice = get_choice()
  if choice == 1:
    do_choice_1()
  elif choice == 2:
    do_choice_2()
  elif choice == 3:
    do_choice_3()
  restart = get_restart()

The restart line is unindented one level, which brings it out of the
if/elif block, and then it'll get executed regardless of 'choice'. (To
strictly match your original code, you'd need to finish the elif block
with "else: continue", but the code makes at least as good sense
without it, so I'd consider that optional.)

> Thanks for your reply and if you have any ideas for me to improve my coding that will prevent me from learning python in a sloppy way. I'd like to learn it correctly the first time!

You're doing fine. The general pattern of programming is:

while True:
  write_code()
  try:
    figure_out_what_is_wrong_with_code()
  except LackOfSkillException:
    mail("python-list@python.org",smart_question())
  run_code()

So far, looks like you're following it just fine. :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#43195

Fromthomasancilleri@gmail.com
Date2013-04-09 09:24 -0700
Message-ID<1629a072-c512-439c-b751-c44f8e945c2e@googlegroups.com>
In reply to#43191
For system version I get this:
2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]

Also, I understand what your saying about the continuation code. There's no need for me to include it in each if/else statement, I could just use it at the end of the program outside of the statements and it would run no matter what. But, what I don't understand exactly is the while statement. I've been looking around a lot lately and notice that people say to use for instance:

while restart: or while true: or while restart = true:

What makes a statement true? Is there a way to return a true or false message. My method was to ask the user to type "true" and if that print statement matched restart = "true" then the loop would continue but i imagine there is a better way then matching strings and integers like i have been.

Also what confuses me is that python doesn't use brackets. How do I contain all of my if/else statements into one while loop? Do I have to indent each line of code and extra indentation? I'm used to highschool doing c++ and java when I would just say:

while (x<3) 
{
     if ()
    else ()
}

[toc] | [prev] | [next] | [standalone]


#43198

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-04-09 12:36 -0400
Message-ID<mailman.365.1365525420.3114.python-list@python.org>
In reply to#43195

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

On Tue, Apr 9, 2013 at 12:24 PM, <thomasancilleri@gmail.com> wrote:

> For system version I get this:
> 2.7.2 (default, Oct 11 2012, 20:14:37)
> [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]
>
> Also, I understand what your saying about the continuation code. There's
> no need for me to include it in each if/else statement, I could just use it
> at the end of the program outside of the statements and it would run no
> matter what. But, what I don't understand exactly is the while statement.
> I've been looking around a lot lately and notice that people say to use for
> instance:
>
> while restart: or while true: or while restart = true:
>
> What makes a statement true? Is there a way to return a true or false
> message. My method was to ask the user to type "true" and if that print
> statement matched restart = "true" then the loop would continue but i
> imagine there is a better way then matching strings and integers like i
> have been.
>
> Also what confuses me is that python doesn't use brackets. How do I
> contain all of my if/else statements into one while loop? Do I have to
> indent each line of code and extra indentation? I'm used to highschool
> doing c++ and java when I would just say:
>
> Python uses indentation.  Most people set their editor to indent 4 spaces
for each level.  It seems odd to people coming from braces languages, but
you get used to it, and it makes code very readable.

As to True/False.  There is a boolean type and its values are True and
False (note the capital letter).  But other values can be considered
True/False.  For instance 0 is considered false, and empty string is
considered false.  Any other number is considered true as is any string
that isn't empty.  Empty sequences are considered false (Tuples, lists)

> while (x<3)
> {
>      if ()
>     else ()
> }
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#43199

FromChris Angelico <rosuav@gmail.com>
Date2013-04-10 02:47 +1000
Message-ID<mailman.366.1365526028.3114.python-list@python.org>
In reply to#43195
On Wed, Apr 10, 2013 at 2:24 AM,  <thomasancilleri@gmail.com> wrote:
> For system version I get this:
> 2.7.2 (default, Oct 11 2012, 20:14:37)
> [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]

Lovely! Perfect.

> But, what I don't understand exactly is the while statement. I've been looking around a lot lately and notice that people say to use for instance:
>
> while restart: or while true: or while restart = true:

while x:
  y

will check whether x "feels trueish", and if it does, will execute y,
then go back and check x again. The general principle is that
something is true and nothing is false - for instance, 0 and 0.0 are
false, while 42 and 0.143 are true. Same goes for lists and such; an
empty list is false, a list with something in it is true.

When you make an actual comparison, you'll get back a result that,
normally, will be one of the strict bool objects True and False. For
instance, the expression:

restart == "true"

will be True if restart has the string "true", and False if it has any
other string.

> What makes a statement true? Is there a way to return a true or false message. My method was to ask the user to type "true" and if that print statement matched restart = "true" then the loop would continue but i imagine there is a better way then matching strings and integers like i have been.

You can print anything, even the boolean values! :) Try it!

Your method works fine. Since you're getting something with
raw_input(), you're working with strings; whatever the user enters,
that's what you work with. You could make it more friendly by checking
just the first letter and case insensitively, and making it "Continue?
Y/N", but that's optional.

> Also what confuses me is that python doesn't use brackets. How do I contain all of my if/else statements into one while loop? Do I have to indent each line of code and extra indentation? I'm used to highschool doing c++ and java when I would just say:
>
> while (x<3)
> {
>      if ()
>     else ()
> }

It's conventional in C++ to indent every block of code.

int main()
{
    //Indent one level
    initialize()
    while (...)
    {
        //Indent two levels
        do_stuff()
        if (...)
        {
            //Indent three levels
            do_more_stuff()
        }
        do_less_stuff()
    }
    close_all()
}

Now, just delete all those lines with nothing but braces. You can
still see the program's logical structure:

int main()
    //Indent one level
    initialize()
    while (...)
        //Indent two levels
        do_stuff()
        if (...)
            //Indent three levels
            do_more_stuff()
        do_less_stuff()
    close_all()

This is how Python works. (And it's almost legal Python syntax, too.
Add a few colons, fix the comments, pretty much done.) For better or
for worse, Python depends on the indentation; but 99%+ of the time,
you would have that indentation even if it didn't matter. (Personally,
I prefer explicit braces. The duplicated information at times helps
catch bugs, and sometimes I format code according to a logical
structure that doesn't necessarily match its physical structure. It's
a freedom I don't often make use of, but it's one that Python denies
me... as I said, for better or for worse. There are those who argue
that that's a freedom I shouldn't have.)

ChrisA

[toc] | [prev] | [next] | [standalone]


#43201

Fromthomasancilleri@gmail.com
Date2013-04-09 09:57 -0700
Message-ID<fb27b1e2-6c94-45ae-ac3e-17a4595f4e97@googlegroups.com>
In reply to#43199
I responded before I saw this message, this was very helpful so I appreciate your quick and helpful responses. So do you think prompting for a string and then checking if the string is true is a good practice for something like this? When would checking for true/false be necessary?

[toc] | [prev] | [next] | [standalone]


#43203

FromChris Angelico <rosuav@gmail.com>
Date2013-04-10 03:08 +1000
Message-ID<mailman.368.1365527293.3114.python-list@python.org>
In reply to#43201
On Wed, Apr 10, 2013 at 2:57 AM,  <thomasancilleri@gmail.com> wrote:
> I responded before I saw this message, this was very helpful so I appreciate your quick and helpful responses. So do you think prompting for a string and then checking if the string is true is a good practice for something like this? When would checking for true/false be necessary?

Prompting for a string is fairly standard. Just figure out what sort
of string you want to look for.

Play around with other programs with similar interfaces, and decide
what you like. Anything you want to do can be done; most of them won't
even be difficult.

ChrisA

[toc] | [prev] | [next] | [standalone]


#43207

FromDave Angel <davea@davea.name>
Date2013-04-09 13:27 -0400
Message-ID<mailman.371.1365528500.3114.python-list@python.org>
In reply to#43201
On 04/09/2013 12:57 PM, thomasancilleri@gmail.com wrote:
> I responded before I saw this message, this was very helpful so I appreciate your quick and helpful responses. So do you think prompting for a string and then checking if the string is true is a good practice for something like this? When would checking for true/false be necessary?
>

No, DON'T check for the string to be true, check if it matches the 
requirements.  Word the question for the user's convenience, not the 
programming language's.  Don't ask for true and false, ask "Continue?" 
and accept "Y" or "N".  Or ask "Q for quit".  Or whatever.  Make your 
comparison case-insensitive, and permit one-character responses.

continue = "y"
while continue[:1].lower() == "y":
     do some work
     continue = raw_input("Do you want to continue (y/n)?"

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#43202

Fromthomasancilleri@gmail.com
Date2013-04-09 09:57 -0700
Message-ID<mailman.367.1365526628.3114.python-list@python.org>
In reply to#43199
I responded before I saw this message, this was very helpful so I appreciate your quick and helpful responses. So do you think prompting for a string and then checking if the string is true is a good practice for something like this? When would checking for true/false be necessary?

[toc] | [prev] | [next] | [standalone]


#43196

Fromthomasancilleri@gmail.com
Date2013-04-09 09:24 -0700
Message-ID<mailman.363.1365524664.3114.python-list@python.org>
In reply to#43191
For system version I get this:
2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]

Also, I understand what your saying about the continuation code. There's no need for me to include it in each if/else statement, I could just use it at the end of the program outside of the statements and it would run no matter what. But, what I don't understand exactly is the while statement. I've been looking around a lot lately and notice that people say to use for instance:

while restart: or while true: or while restart = true:

What makes a statement true? Is there a way to return a true or false message. My method was to ask the user to type "true" and if that print statement matched restart = "true" then the loop would continue but i imagine there is a better way then matching strings and integers like i have been.

Also what confuses me is that python doesn't use brackets. How do I contain all of my if/else statements into one while loop? Do I have to indent each line of code and extra indentation? I'm used to highschool doing c++ and java when I would just say:

while (x<3) 
{
     if ()
    else ()
}

[toc] | [prev] | [next] | [standalone]


#43219

FromWalter Hurry <walterhurry@lavabit.com>
Date2013-04-09 19:35 +0000
Message-ID<kk1qiq$vpt$1@news.albasani.net>
In reply to#43191
On Wed, 10 Apr 2013 02:10:29 +1000, Chris Angelico wrote:

> On Wed, Apr 10, 2013 at 1:47 AM,  <thomasancilleri@gmail.com> wrote:
>> ... I'm not sure what version I'm using ...
> 
> Try putting these lines into a Python script:
> 
> import sys
> print(sys.version)
> 
That works (of course), but in every Python version I've seen, one merely 
needs to invoke the python interactive interpreter and the banner is 
displayed:

$ python
Python 2.7.3 (default, Aug  9 2012, 17:23:57) 
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web