GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Feb-2008, 20:02
fonz87 fonz87 is offline
New Member
 
Join Date: Jan 2008
Posts: 12
fonz87 has a little shameless behaviour in the past

little help part 2


ok so i need help with some codes.
I need to write a program that takes as input a positive integer n and generates the following shape.
so for example if i put in n=5
it would come out like this
1
1 3
1 3 5
1 3 5 7
1 3 5 7 9
i have this so far but it's coming like this. where it producing doubles
1
1
1 3
1 3
1 3 5
1 3 5
so if anyone knows how to fix it.. that be awesome.
Python Code:
def main():
    x=input("Enter a number")
    for i in range(1,x+1):
            for i in range(1,i+1):
                if(i%2==1):
                     print i,
            print
main()
the teacher told me to get rid of the if(i%2==1): and the print i and replace the i in the range with j but still need to add a code where it does odd integers
anyone knows the code, that would be awesome.
Last edited by LuciWiz : 13-Feb-2008 at 12:10. Reason: Please insert your Python code between [py] & [/py] tags
  #2  
Old 13-Feb-2008, 12:42
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,534
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: little help part 2


Try removing the second for statement. You have to keep the modulus statement, otherwise you won't get your odd integers. I also didn't worry about changing the "i" to a "j"; it shouldn't matter in this situation.

I just tried it and it worked correctly for me. Don't forget your indentation changes.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 13-Feb-2008, 14:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: little help part 2


Quote:
Originally Posted by fonz87
ok so i need help with some codes....where it does odd integers

Here's the way I look at it:

The first loop (the outer loop) determines how many rows you are going to print.
If the user enters 5, you are going to print five rows:
Python Code:
    x=input("Enter a number: ")
    for i in range(1,x+1):
.
.
.

Now, for each row, i, you want the inner loop to print i items. That is, for row number 1, it will print one item. For row number, 2 it will print two items, etc.:

Python Code:
    x=input("Enter a number: ")
    for i in range(1,x+1):
            for j in range(1,i+1):
.
.
.

So far, so good. OK? If you just printed the value of j, each time through the inner loop, it would print i integers:
1, 2, ..., i

So, how do you make it print that many odd integers on each row? (Remember, the number of items is correct, so you don't need to change the loop limits; you want to print the correct values.)

Simple:

Python Code:
    x=input("Enter a number: ")
    for i in range(1,x+1):
        for j in range(1,i+1):
            print(2*j-1),
.
.
.


See how it works?

On any given row:

when j = 1, then it prints '1',
when j = 2, then it prints '3',
when j = 3, then it prints '5',
etc.

Regards,

Dave


Footnote:
As crystallattice mentioned, the inner loop variable can remain i, since it becomes a completely new variable. Each time through the loop it gets initialized to 1 in its range statement (and the upper limit is calculated from the value of outer loop i+1), but it doesn't conflict with it inside the inner loop.

However...

If my professor told me to change it to j, I would change it to j. It doesn't work any better (or worse, for that matter) from python's point of view, but it might help not to confuse mere mortals like you and me (also your professor, assuming your professor is a mortal).

Little "human engineering" stuff like that might make the code easier to maintain. Especially since certain other programming languages (and their programmers) would really get screwed up if you use the same name for an inner loop counter and an outer loop counter.
  #4  
Old 13-Feb-2008, 19:32
fonz87 fonz87 is offline
New Member
 
Join Date: Jan 2008
Posts: 12
fonz87 has a little shameless behaviour in the past

Re: little help part 2


ok thank you. What i was trying to do in class today was changing the

for j in the range(1,j+1):
and from there doing something with the if(i%2==1), something something.. not sure i was going to play around with this. alot of this stuff.. i really dont understand cuz my professor sucks at teaching.. no one in the class likes his teaching.. all he does is explain a little of the chapter .. like what is lists.. and then He asks us to do a problem... he doesn't even show us how to do it.. or a example or anything.. like we r suppose to know everything! im like wtf.. r u serious.

But thanks alot all of you guys. this helps me..
  #5  
Old 13-Feb-2008, 21:05
fonz87 fonz87 is offline
New Member
 
Join Date: Jan 2008
Posts: 12
fonz87 has a little shameless behaviour in the past

Re: little help part 2


ok now i need help in this one... i got it to work almost..
it's asking me to compute the grade of students from file and then it's asking me to compute the students grade and have a class average.
this is what i have so far
import math
import string

def main():

#It will compute the grades and output it in the finalgrades file
infile=open("textfile.txt",'r')
outfile=open("finalgrades.txt",'w')
names=infile.readlines()
count=0
for line in names:
fname=string.split(line)[0]
lname=string.split(line)[1]
test1=string.split(line)[2]
test2=string.split(line)[3]
test3=string.split(line)[4]
total=((eval(test1)+eval(test2)+eval(test3))/3.0)

count=count+1
total=float(total)
totalavg=total/count
print fname, lname, total
print totalavg
main()

its printing the students name and grade but the total average is printing out all werid .. like doubles and some other number
  #6  
Old 14-Feb-2008, 07:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: little help part 2


Quote:
Originally Posted by fonz87
ok now i need help in this one...
its printing the students name and grade but the total average is printing out all werid .. like doubles and some other number

You have noticed that your program does not give correct results. What to do? What to do?

My suggestion:

Look at what you are doing do calculate the overall average. Shouldn't it be something more like the following?


Code:
Sum = 0 Count = 0 Perform the following loop for all lines in the file: Calculate and print This student's average Sum = Sum + This student's average Count = Count + 1 Ovarall average = Sum / Count


Regards,

Dave

Footnote: When you have a question about a new topic, it is appropriate to start a new thread rather than just continuing to add new stuff to a previous thread. It might help people using a searching to look for threads with programming tips to give an appropriate title rather than just something about asking for help. Something like "Printing Triangle of Odd Numbers", or "Calculating Class Average" or some such thing.
  #7  
Old 14-Feb-2008, 10:21
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,534
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: little help part 2


BTW, thanks for correcting me. I didn't notice that my way (simply removing the the second for statement) prints the numbers in a single column, rather than multiple rows.

Though I must say, I'm happy that I was able to make it do something. I haven't actively programmed for more than a year now; too busy finishing my Master's degree.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
floating point decimal to ascii conversion crazypal C Programming Language 5 18-Apr-2007 04:59
Re: Multipart Articles on GIDNetwork, Part 2 admin MySQL / PHP Forum 0 06-Jan-2006 04:26
Re: Things to Avoid in C/C++ -- scanf / epilogue, Part 9 WaltP C Programming Language 0 01-Oct-2005 05:24
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:11.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.