ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 " ").
#!/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?
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).
#!/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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.