LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2008, 03:39 PM   #1
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
python exit loop by 'enter'


hi,

I want the user to enter integers as long as they want (and append them to the list), however, once you just press enter (without any integer) the program should go out of the loop to e.g. some other function.
So far I've written the following:

Code:
marks = []
single_mark = 1

while single_mark != " ":
    single_mark = int(raw_input("enter your mark: "))
    marks.append(single_mark)
Code:
while single_mark != " ";
was supposed to mean 'continue the loop until the user's input is not just 'enter' - It doesn't work as I meant it to work.

Any hints, thanks
 
Old 08-27-2008, 05:14 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
You set single_mark to be the result of int(), so there is no way it could ever be equal to " " or any other string. You need to test the returned value of raw_input against "" (not " ").
 
Old 08-27-2008, 05:41 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Two things:
  1. Pressing enter ('') is not the same as typing a space and pressing enter (' ').
  2. You cannot (without exception handling) int() a non-numerical string (and '' happens to be a non-numerical string).
Here’s what I might do (if you want to avoid exception handling):
Code:
while True:
    single_mark = raw_input("enter your mark: ")
    if single_mark == '':
        break
    marks.append(int(single_mark))
 
Old 08-28-2008, 02:56 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836

Original Poster
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
hi, thanks for your replies.

I completed what I wanted:

Code:
#!/usr/bin/env python

marks = []
sum = 0

while True:
    single_mark = raw_input("enter your mark: ")
    if single_mark == '':
        break
    elif int(single_mark) > 6:
        print "The mark scale is 2-6"
    elif int(single_mark) < 2:
        print "The mark scale is 2-6"
    else:
        marks.append(int(single_mark))
        print "The mark has been added to the database"


print "You entered %d marks" % len(marks)
print "Your marks are as follows: "
x = 0
for x in marks:
    print x
    sum += x

average = float(sum) / len(marks)

print "Your average mark is %f" % average
I'm a python beginner so can you tell me what you would improve in this simple code?

thanks
 
Old 08-28-2008, 06:13 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by sycamorex View Post
I'm a python beginner so can you tell me what you would improve in this simple code?
A few things:
  • You shouldn’t need to pre-declare x.
  • Since the second and third conditionals have an identical “THEN” block, you can combine them with a logical OR.
  • You are calling int(single_mark) three times. The python interpreter will (or should) optimize this into a single call whose result is kept in a register, but you can’t always count on the interpreter to clean up your inefficiencies.
  • Stylistically, you can separate the second and third conditionals (which should be combined as stated above) into their own if (there is no need for elif since a break effectively ends that code pathway).
 
Old 08-29-2008, 05:15 PM   #6
Shadowmeph
Member
 
Registered: May 2008
Location: West Coast Canada
Posts: 282

Rep: Reputation: 29
Quote:
Originally Posted by sycamorex View Post
hi, thanks for your replies.

I completed what I wanted:

Code:
#!/usr/bin/env python

marks = []
sum = 0

while True:
    single_mark = raw_input("enter your mark: ")
    if single_mark == '':
        break
    elif int(single_mark) > 6:
        print "The mark scale is 2-6"
    elif int(single_mark) < 2:
        print "The mark scale is 2-6"
    else:
        marks.append(int(single_mark))
        print "The mark has been added to the database"


print "You entered %d marks" % len(marks)
print "Your marks are as follows: "
x = 0
for x in marks:
    print x
    sum += x

average = float(sum) / len(marks)

print "Your average mark is %f" % average
I'm a python beginner so can you tell me what you would improve in this simple code?

thanks
I am interested in Learning python I am wondering where are you learning this?
 
Old 08-29-2008, 05:57 PM   #7
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
Quote:
Originally Posted by Shadowmeph View Post
I am interested in Learning python I am wondering where are you learning this?
There are a ton of great sites out there for python...

http://www.python.org (check out the Tutor mailing list...it's active and the people there are quite friendly...)
http://www.uselesspython.com/tutorials.php

Most Linux distributions have a package of "Dive Into Python", a great ebook on Python (or you can get it here: http://www.diveintopython.org/)

Numerous Python books:
Learning Python
Python in a Nutshell
etc...
 
Old 08-29-2008, 09:44 PM   #8
Shadowmeph
Member
 
Registered: May 2008
Location: West Coast Canada
Posts: 282

Rep: Reputation: 29
Quote:
Originally Posted by rocket357 View Post
There are a ton of great sites out there for python...

http://www.python.org (check out the Tutor mailing list...it's active and the people there are quite friendly...)
http://www.uselesspython.com/tutorials.php

Most Linux distributions have a package of "Dive Into Python", a great ebook on Python (or you can get it here: http://www.diveintopython.org/)

Numerous Python books:
Learning Python
Python in a Nutshell
etc...
Thanks I will check those out
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: can I see which iteration I'm on in a loop? BrianK Programming 2 08-27-2008 09:01 PM
optional exit from loop, shell script RudraB Programming 2 07-17-2008 03:30 AM
Newbie - How to exit CUI and enter GUI seanpearman Fedora 7 12-31-2006 10:43 PM
Embedding Python in C - adding a exit routine to a module? redarrow Programming 0 08-10-2005 01:20 PM
How can I enter php interface like "python[enter]" in python backpacker Linux - Software 1 06-20-2005 07:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration