School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,492 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,826 people online right now. Registration is fast and FREE... Join Now!




Setting initial minimum value

 

Setting initial minimum value, Simple question

49erfan

30 May, 2009 - 07:06 PM
Post #1

New D.I.C Head
*

Joined: 1 Mar, 2009
Posts: 26

I just picked up a python book and I have a stupid question. Obviously I can just set the initial high value to 0, but I need to have the initial low value set to the first value entered. How do I go about doing this? Thanks in advance for any help.
CODE
total = 0
count = 0
low   = 0
high  = 0
a     = []

while True:
    line = input("enter a number or press Enter to finish: ")
    if line:
        try:
            number = int(line)
        except ValueError as err:
            print("Enter an integer")
            continue
        list.append(a, number)
        if number > high:
            high = number
        if number < low:
            low = number
        total += number
        count += 1
    else:
        break

if count:
    print("count =", count, "\n" +
          "total =", total, "\n" +
          "mean  =", total / count, "\n" +
          "low:  ", low, "\n" +
          "high: ", high, "\n" +
          "numbers: ", a, "\n")



User is offlineProfile CardPM
+Quote Post


code_m

RE: Setting Initial Minimum Value

31 May, 2009 - 12:38 PM
Post #2

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 118



Thanked: 8 times
My Contributions
I see that your using Python 3, what book are you using? If it's written by Mark Summerfield I have the same book, and in that case, just keep reading.

Otherwise, to set up a low variable set to the first input I would:
python

total, count, high = 0, 0, 0
a = []

while True:
while True:
line = input("Enter a value (Enter to finish): ")
if line:
try:
low = int(line)
except ValueError as err:
print(err)
continue
else:
exit()
line = input("Enter a value (Enter to finish): ")
...

I use "..." to represent the rest of the code you already have.

You may notice that the loops to initialize low looks a lot like the code you already have, and this is fine for now, but as you learn more you want to use defined functions (these call pre-written code).

Also you see that in the else statement of setting low, I call exit(), this is because you must leave both while loops. Normally you do not call exit() or quit() within your program, but again here it is fine, later you will see if you want to exit you should call sys.exit(), and you have not come that far yet.

This post has been edited by code_m: 31 May, 2009 - 12:43 PM
User is offlineProfile CardPM
+Quote Post

49erfan

RE: Setting Initial Minimum Value

1 Jun, 2009 - 10:41 AM
Post #3

New D.I.C Head
*

Joined: 1 Mar, 2009
Posts: 26

Thanks for the help; I came up with something similar yesterday morning (but mine looks a bit messier). For some reason it didn't quite feel right to use a try statement in that situation, so I am glad to see that you went about it the same way.
User is offlineProfile CardPM
+Quote Post

code_m

RE: Setting Initial Minimum Value

1 Jun, 2009 - 06:40 PM
Post #4

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 118



Thanked: 8 times
My Contributions
Glad to be of help, keep in mind that the python developers don't want the general public top use python3 yet, so you should say that you are using python3 when asking for help. Personally, I don't understand why the python developers are not pushing python3 because it's just that much better.

Also, remember that this is not the best solution, but as a beginner it's fine. It will take plenty of time and effert to start writing "production quality code"
User is offlineProfile CardPM
+Quote Post

XPherior

RE: Setting Initial Minimum Value

5 Jun, 2009 - 06:16 PM
Post #5

New D.I.C Head
Group Icon

Joined: 21 May, 2008
Posts: 7


Dream Kudos: 25
My Contributions
I hope this doesn't fly over your head since you're new. I learn best by example, though, maybe you will too!

CODE


>>> Numbers = [1, 63, 334, 24, 95]
>>> print('Sum: ' + str(sum(Numbers)))
Sum: 517
>>> print('Min: ' + str(min(Numbers)))
Min: 1
>>> print('Max: ' + str(max(Numbers)))
Max: 334
>>> print('Count: ' + str(len(Numbers)))
Count: 5



I made use of the 'built-in functions' in Python. You can see what they do, pretty easily. =o Again, if this went over your head, sorry. Maybe you'll come back to it later and it will make sense. smile.gif
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Setting Initial Minimum Value

6 Jun, 2009 - 02:55 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,261



Thanked: 389 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
When doing something with high and low of a collection, always initialize both to the first value given. That way, you never have to worry about it. Also, I'm not entirely sure why you're maintaining a count...

python

a.append(number)
if len(a)==1:
low = number
high = number
else:
if number > high:
high = number
if number < low:
low = number
total += number


There are also built in min and max functions, e.g. "min(a)".

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:31AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month