Python Consider a Python GUI program that produces a window with the following w
ID: 3842074 • Letter: P
Question
Python
Consider a Python GUI program that produces a window with the following widgets:
a text box to display the value of one element of a given list (e.g., a list of the five vowels);
a button to retrieve the previous value in that list (if there is one). This button is disabled if there is no previous value in the list.
a button to retrieve the next value in that list (if there is one). This button is disabled if there is no next value in the list.
a label to display the number of the item being displayed and the total number of items (e.g., "1/5")
The initial output is as shown below: Vowels 7K prev next 1/5 Hitting the next" button once (from the initial state above) produces the following output vowels 7A prev next 2/5 Hitting the next" button three more times produces the following output Vowels 76 prev next 5/5 Most of the back-end of the program has already been written. Amongst other things, the following variables have been created Window Tk keeps track of the item being displayed counter 0 The following functions have also been completed previous item f display the previous item (if any) next item display the next item (if any)Explanation / Answer
def productOfOdds(tup):
prod = 1
if tup[0]%2 == 1: # if th efist element in tuple is odd
prod = tup[0]
if len(tup) > 1: #if more than 1 element is present
prod = prod * productOfOdds(tup[1:]) #call recursively with rest of elements
return prod;
#------------------------------
def printStarBucks(n):
star = '*' #define the star character
dollar = '$' #deffine the dollar character
if n > 0: #make sure n is +ve
print star*n,dollar*n # the * operator is used for repeatition
return;
#------------------------------
def factorial(n):
#recursive function to calculate factorial of number n
if n > 1: # for number n which is greater than 1, it is n * factorial of previous
fact = n * factorial (n-1)
else:
fact = 1
return fact;
#------------------------------
def oddevenfact(n):
fact = 1 #1 will be returned when n is 0 or less
if n > 1:
# for odd numbers recursive function will stop at 1 and and for even numbers
# last recursive call will be with n = 0 which will return 1
fact = n * oddevenfact (n-2)
return fact;
#------------------------------
def equals(x,y):
#A function to check if x and y are equal using recursion
if x < 0 or y < 0: #when one of them becomes negative also function terminates
value = False
else:
if x==0 and y==0: #the recursive function call terminates at this condition
value = True
else:
value = equals(x-1, y-1) #call recursively by decreasing both numbers
return value;
#------------------------------
def printStars(n):
#function to print * symbol n times
star='*'
print star*n
return;
#------------------------------
def printTriangle(n):
#prints * in triangle form with last line having n *s
for i in range(n+1): #range(n+1) gives numbers from 1 to n
printStars(i)
return;
#------------------------------
#testing productOfOdds
t=(5,2,7) #first element odd and total 2 odd elements
val=productOfOdds(t)
print 'productOfOdds' ,t ,'=' , val
t=(3,) #only one odd number
val=productOfOdds(t)
print 'productOfOdds' ,t ,'=' , val
t=(3,5) #all odd elements
val=productOfOdds(t)
print 'productOfOdds' ,t ,'=' , val
#testing printStarBucks
print 'calling printStartBucks(3)'
printStarBucks(3) # prints ***$$$
print 'calling printStarBucks(0)'
printStarBucks(0) # prints nothing
#testing factorial
print 'factorial(1)=' ,factorial(1 ) #should print 1
print 'factorial(3)=' ,factorial(3) #should print 6
print 'factorial(5)=' ,factorial(5) #should print 120
#test oddevenfact
print 'oddevenfact(6)=' ,oddevenfact(6) #should print 6*4*2 ie. 48
print 'oddevenfact(7)=' ,oddevenfact(7) #should print 7*5*3*1 i.e 105
#testing equals
print 'equals(5,5)=' , equals(5,5) #should print true
print 'equals(3,4)=' , equals(3,4) #should print false
#testing printTriangle
printTriangle(3)
printTriangle(5)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.