Having trouble with problem set 1, problem 1: I have a program that calculates every odd number, through a 'while' loop. I can do a conditional test to check if each number generated is divisible by 3, 5, 7, etc. but I can't make it so that a new test is added each time a new number is printed: x=0 x=x+1 n=1 while n<1000: x=x+2 if x%3!=0 or x==3: print (x) n=n+1 How can I make it so that each time a number is printed, that number gets added to a list of tests that will be done on the next iteration of the loop? Help!
I don't know exactly which course this is from, and I don't know if you have studied methods yet. For that matter, I don't know if you have studied tuples or lists yet either. However, assuming that the easiest of these is a tuple, I will try to help you with this problem. Every time you find a prime number, you can add it to your tuple. So for this first ten numbers your tuples would look like this: 1: (,) 2: (2,) 3: (2,3) 4: (2, 3) 5: (2, 3, 5) 6: (2, 3, 5) 7: (2, 3, 5, 7) 8: (2, 3, 5, 7) 9: (2, 3, 5, 7) 10: (2, 3, 5, 7) Now, in python, you can iterate over elements of a tuple. Something like: for prime in primesTuple: test divisibility Hopefully this helps a little bit without giving it away.
Tuples are immutable (unchangeable). Lists are mutable. If you're going to be adding to it, a list is more suitable. You can fake adding to a tuple, but you're actually creating a new tuple with the elements of your old tuple and what you added.
Absolutely true, but tuples are often introduced first in CS courses, and I wanted to keep it as likely as possible that he could follow me.
Let me rephrase, in my CS intro course, tuples were introduced first.
actually ye for that assignment list arent yet taught...so i suppose tuples it should be:).
Just to clarify (I'm new to OpenStudy, so I don't know who's on this site or how it works), I'm going through the MIT 6.00 Intro to CS videos
Also, thank you very much to everyone. I think I'm close. However, there's one crucial part that is missing: primeTuple= (2,) x=0 n=1 x=x+1 while n<300: x=x+2 for test in primeTuple: if x%test!=0 or x==test: print (x) newTuple=(x,) primeTuple= primeTuple+newTuple n=n+1 What is happening now is that when I run it, each number is printed as many times as there are tests. So 3 is printed once, 5 twice (not divisible by two: print. not divisible by 3: print), 7 three times, and so on. How do I make it so that the condition to print x is that x not be divisible by any and all of the numbers?
ok so: 1. x=0,x=x+1 why not directly x=1 ? no point doing that. 2.(not a mistake just a python way to write it) x=x+2 u can write it x+=2. 3.to get your primeTuple printed you have to put a print statement in the end that asks the code to print primeTuple. 4. x==test i dont think you need it since your x is always bigger than your test.(recheck that maybe im wrong (its 22:00 for me bit sleepy xD) 5.(again not wrong but tidier) newTuple=(x,) primeTuple= primeTuple+newTuple you can write this as: primeTuple+=(x,) 6.now your problem is that a. your code prints all odds! b.it print them multiple times. solution hint: *set a Boolean controll variable to some state( usually True) *in your for loop, control if, for some condition this variable should stay True or change to False.... **if False get out of the loop.... **if True : add x to primeTuple and n+=1 hope this helps!if you need more help ask so:)
Thank you for your help, I figured it out! Very proud of myself. The code I ended up with might be a little cluttered, but it was my first, and it got the job done: primeTuple= (2,) tupleNumber=1 x=1 n=1 while n<1000: x+=2 testNumber=0 for test in primeTuple: if x%test!=0 or x==test: testNumber+=1 if tupleNumber==testNumber: #this makes it so all the tests are run #before the x prints print (x) primeTuple+= (x,) tupleNumber+=1 n+=1 primeTuple+= (x,) tupleNumber+=1 n+=1
well ull learn to write "cleaner" codes...we all will! mine are not a lot better but im learning! good job for the code!:)
Join our real-time social learning platform and learn together with your friends!