The Artima Developer Community
Sponsored Link

Python Answers Forum
adding while loop outputs?

4 replies on 1 page. Most recent reply: Aug 28, 2004 1:02 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
John Avery

Posts: 6
Nickname: kingdud
Registered: Jul, 2004

adding while loop outputs? Posted: Aug 26, 2004 5:20 PM
Reply to this message Reply
Advertisement
I am trying to figure out if there is a way to add together the outputs of a while loop. Here is the code for the loop I have:

charlvl = input("Character level? ")
while charlvl != 0:
mobxp = (charlvl*5) + 45
a = int((8*charlvl*mobxp)/100)*100
charlvl = charlvl - 1
print "At level", charlvl,"you will have earned", a, "xp total."

What I am trying to do is have someone (me) put in their character level, then have it calculate how much XP that level will take (the mobxp = (charlvl*5) + 45 & a = int((8*charlvl*mobxp)/100)*100 line do that), and every level's XP below that level (lowest level is 1). Then, if it is possible, add those outputs together.

If you want to compile and run this, the output for "character level? 2" should be 800 on first run and 400 on second run. When recoded I the hoped for output would be one line and would read something like "At level 2, you will have earned 1200 xp."

Notes:
1. Don't worry about the character level being negative, if someone is dumb enough to put in a negative number, they deserve the output.
2. I realize a while loop probably won't do that, but the tutorials I have read for python don't show me any other way to do it :(
3. The starting level is 1, there is no need to calculate a level up for level 0 as it doesn't exist.
4. If you need something further explained, please ask and I will see what I can do.

Peace out,

-John


John Avery

Posts: 6
Nickname: kingdud
Registered: Jul, 2004

Re: adding while loop outputs? Posted: Aug 26, 2004 5:25 PM
Reply to this message Reply
hm...I wish I could edit my post, oh well. As a note, if you run the above code as it stands, the output is "at level 1..." and "at level 0..." this is because I am new to programming and forgot that computers count from zero :p. My bad.

That makes level 0 effectivly level 1.

Peace out,

-John

John Avery

Posts: 6
Nickname: kingdud
Registered: Jul, 2004

Re: adding while loop outputs? Posted: Aug 26, 2004 5:44 PM
Reply to this message Reply
Sorry to waste topic space in here. I just solved my own problem...for anyone interested the solution was this:

list = []
while charlvl != 0:
mobxp = (charlvl*5) + 45
a = int((8*charlvl*mobxp)/100)*100
charlvl = charlvl - 1
list.append(a)
b=sum(list)
print "At level", (charlvl + len(list)),"you will have earned", b, "xp."

Sorry for wasting your time >.<

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: adding while loop outputs? Posted: Aug 26, 2004 9:03 PM
Reply to this message Reply
Good job man, just for future reference though,
please indent your code coz its really hard to
go through and people would probably get frustrated
and leave it as if they can't read your code.
or [pre]
def func:
statement1
or [/pre]( code in [] paranthesis
or pre in[] closed by [/]

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: adding while loop outputs? Posted: Aug 28, 2004 1:02 PM
Reply to this message Reply
Better yet, for Python code, just use the pre tags, for example:

def fib(n):
for i in range(len(fibs),n+1):
fibs.append( fibs[-1] + fibs[-2] )
return fibs[n]

Flat View: This topic has 4 replies on 1 page
Topic: unlimited precision? Previous Topic   Next Topic Topic: my_button[

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use