Python 1 A little help with this one. Im a bit lost on how to achieve the result
ID: 3860666 • Letter: P
Question
Python 1
A little help with this one. Im a bit lost on how to achieve the results I need.
Take the two code sections below and combine it so that the user input and the outputs are in a graphics window.
Modify the prompt for the integer to display in the window and also modify the output to display in the window. The code that does the actual calculations should be good as is.
from graphics import *
def main():
win = GraphWin("Sample Input", 300, 400)
win.setCoords(0,0, 299, 399) # not required, just convenient
upleft= Text(Point(50, 350),"50, 350")
upleft.setStyle('bold italic') # not required, just convenient
upleft.draw(win)
upright= Text(Point(250, 350),"350, 350")
upright.setStyle('bold italic') # not required, just convenient
upright.draw(win)
downleft= Text(Point(50, 50),"50, 50")
downleft.setStyle('bold italic') # not required, just convenient
downleft.draw(win)
downright= Text(Point(250, 50),"350, 50")
downright.setStyle('bold italic') # not required, just convenient
downright.draw(win)
prompt = Text(Point(150, 50),"Click to end")
prompt.draw(win)
win.getMouse()
win.close()
main()
---------------------------------------------------------------------
FicFib Code
def main():
enteredNum = int(input('Enter a number: '))
# sum
sum = 0
for i in range(1, enteredNum+1):
sum += i
print('Sum of integers from 1 to',enteredNum, 'is:',sum)
# factor
i = 1
fact = 1
while i <= enteredNum:
fact *= i
i += 1
print('Factor of number',enteredNum,'is:',fact)
# fib
a = 0
b = 1
# if input number is less than 2 print and return
if(enteredNum < 2):
print('Fibonacci series of number',enteredNum,'is:')
print(a)
return
# if input number is 2 or larger continue through
i = 2
print('Fibonacci series of number',enteredNum,'is:')
print(a)
print(b)
while i < enteredNum:
c = a + b
a = b
b = c
i += 1
print(c)
if __name__=='__main__':
main()
Explanation / Answer
def main():
win = GraphWin("Sample Input", 300, 400)
win.setCoords(0,0, 299, 399) # not required, just convenient
upleft= Text(Point(50, 350),"50, 350")
upleft.setStyle('bold italic') # not required, just convenient
upleft.draw(win)
upright= Text(Point(250, 350),"350, 350")
upright.setStyle('bold italic') # not required, just convenient
upright.draw(win)
downleft= Text(Point(50, 50),"50, 50")
downleft.setStyle('bold italic') # not required, just convenient
downleft.draw(win)
downright= Text(Point(250, 50),"350, 50")
downright.setStyle('bold italic') # not required, just convenient
downright.draw(win)
prompt = Text(Point(150, 50),"Click to end")
prompt.draw(win)
win.getMouse()
win.close()
enteredNum = int(input('Enter a number: '))
# sum
sum = 0
for i in range(1, enteredNum+1):
sum += i
print('Sum of integers from 1 to',enteredNum, 'is:',sum)
# factor
i = 1
fact = 1
while i <= enteredNum:
fact *= i
i += 1
print('Factor of number',enteredNum,'is:',fact)
# fib
a = 0
b = 1
# if input number is less than 2 print and return
if(enteredNum < 2):
print('Fibonacci series of number',enteredNum,'is:')
print(a)
return
# if input number is 2 or larger continue through
i = 2
print('Fibonacci series of number',enteredNum,'is:')
print(a)
print(b)
while i < enteredNum:
c = a + b
a = b
b = c
i += 1
print(c)
if __name__ =='__main__':
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.