Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nancy Melucci
Courses Plus Student 36,422 PointsCode retyped exactly from instructor's notes, won't run.
I need a pythonista to help me figure out why this code won't run. It's my prof's. I don't think he wants it debugged. I think there is something wrong with it. I don't want to offend by asking him. Because, you know....
thanks. There are five exercises in all. This is the third. Two others don't run either but maybe if someone points out the error here I can figure out the remaining ones. I just don't have the chops (yet) to do this.
s = "Result:\nLoop1: " # example 1
x = 1 # initial value
while (x < 10): # Boolean expression
s = s + str(x) + " "
x += 1 # increment
s += "\nLoop2: " # example 2
x = 9 # initial value
while (x >= 1): # Boolean expression
s = s + str(x) + " "
x -= 1 # increment
s += "\nLoop3: " # example 3
i = 10
f = 1
while (i >= 1):
f = f * i
i -= 1
s += str(f) + "\n"
s += "\nLoop4: " # example 4
x = (23, 17, 22, 8, 10, 19, 5, 43, 28)
min = x[0]
i = 1 # initial value
while (i < len(x)):
if x[i] < min:
min = x[i]
i += 1 # increment
s += "The smallest value is " + str(min) + "\n"
s += "\nLoop5: " # example 5
InputBox.ShowDialog("Are you nuts? [Yes/No]: ")
x = InputBox.GetInput()
while (x.upper() != "YES"):
InputBox.ShowDialog("Again, are you nuts? ")
x = InputBox.GetInput()
s += "Just kidding!! Thank you.\n"
s += "\nLoop6: " # example 6
s += " 1 2 3 4 5 6 7 8 9\n"
s += "--------------------\n"
for i in range(1, 10):
rst = str(i) + "|"
for j in range(1, 10):
rst += " " + str(i * j)
s += rst + "\n"
###### Message Box Code ######
from tkinter import *
root = Tk()
root.title('Message Box')
Label(root, justify=LEFT, text=s).grid()
root.mainloop()
2 Answers
jesdavpet
21,489 PointsHi Nancy,
Simply put, this code must be debugged. The while loops are running infinitely, because they never reach an exit condition (where the boolean condition evaluates to false).
In Python indentation defines code blocks, and you must change the indentation of a number of statements to get this program to run.
For example, in the first loop:
while (x < 10): # Boolean expression
s = s + str(x) + " "
x += 1 # increment
should be indented so it belongs to the while loop above it, like so:
while (x < 10): # Boolean expression
s = s + str(x) + " "
x += 1 # increment
Hope this helps!
Nancy Melucci
Courses Plus Student 36,422 PointsThank you for the prompt answer. I was using the reformat code function in PyCharm (because I suspected that indentation was part of the problem) but that didn't fix the problem (except PyCharm went from code red to code yellow).
I'll check my transcription too. But I've taken 3 classes with the prof and generally you follow along in 5 exercises for which he gives you the code directly and then do one programming task on your own making use of a concept from the exercises. This was an exercise not a programming challenge. I spent much more time on this than usual and could not fix it.
I am ahead in the class so I suppose if I am diplomatic about it, I'll be doing him and my classmates a favor
thanks. NJM
jesdavpet
21,489 Pointsjesdavpet
21,489 PointsBTW, I doubt your prof would be offended, as long as you don't blame the problem on him when you ask for help.
You probably should bring it up, actually. Because, if it is a problem with the code he distributed -- then everyone else in your course is probably stuck at the same spot -- and that's not good for anyone.