Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 18 Online
OpenStudy (anonymous):

I'm currently finishing Lecture 3 and when I tried to run the Tuple example program: x = 100 divisors = () for b in range(1, 20): if x%b == 0: divisors = divisors + (b)# IDLE keeps resetting. The actual results of the program aren't shown in the lecture video, so I just wanted to know if this was supposed to make IDLE reset or if I did something wrong.

OpenStudy (anonymous):

``` IDLE keeps resetting. ``` What does that mean? Are you writing the code in the shell or in an editing window?

OpenStudy (anonymous):

I wrote the code in an editing window and ran it through the shell. I just realized that the shell reset because I forgot to save my file with a .py, but now when I run it I get: Traceback (most recent call last): File "C:/Users/Awinash/Desktop/MIT CS Python Assignments/Lecture 3 Example.py", line 9, in <module> divisors = divisors + (b)# TypeError: can only concatenate tuple (not "int") to tuple

OpenStudy (anonymous):

The exact code can be seen in the Lecture 3 video at 43:28, though I changed the variable from i to b after it wouldn't work to see if it would change anything.

OpenStudy (anonymous):

What is `(b)` supposed to be? If it is supposed to be a Tuple, then change it to `(b,)` so python knows this. Otherwise, python will think the parenthesis are just there to preserver an order of operations, which makes no sense here since there is no operation inside the parenthesis.

OpenStudy (anonymous):

Another thing: `x += y` is shorthand for `x = x + y`

OpenStudy (anonymous):

Only in custom non-standard objects where the operators are not correctly overridden will there be a difference.

OpenStudy (anonymous):

Yes, it's supposed to be a Tupple and I changed the code to: x = 100 divisors = () for b in range(1, x): if x%b == 0: divisors = divisors + (b,) This restarts the shell. What I'd like to know is if it's supposed to do this or if I made an error. The lecture video doesn't show the result, so I'm just confused about this bit.

OpenStudy (anonymous):

What? It should run the code and return to the shell.

OpenStudy (anonymous):

you don't have to use idle either. You can try python program_name.py in your terminal.

OpenStudy (anonymous):

Just tried to run it from the terminal, but it just opened and immediately closed without doing anything.

OpenStudy (anonymous):

http://repl.it/ Try running with this.

OpenStudy (anonymous):

Honestly, this is an IT problem, not a programming problem.

OpenStudy (anonymous):

That didn't do anything either. You're probably right about this being an IT problem, but I've tried this on different computers and still got the same result.

OpenStudy (rsmith6559):

Your program isn't going to do anything THAT YOU CAN SEE. Try adding this line at the end with no indentation: print divisors BTW, your second version runs fine.

OpenStudy (anonymous):

Just tried it and it worked. Thank you for the help.

OpenStudy (anonymous):

thanks

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!