![]() |
|
#1
|
|||
|
|||
little help part 2ok 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:
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
|
||||
|
||||
Re: little help part 2Try 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
|
|||
|
|||
Re: little help part 2Quote:
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:
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:
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:
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
|
|||
|
|||
Re: little help part 2ok 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
|
|||
|
|||
Re: little help part 2ok 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
|
|||
|
|||
Re: little help part 2Quote:
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:
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
|
||||
|
||||
Re: little help part 2BTW, 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 GIDBlog
2nd Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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