[Link]
com/
CST 362: PROGRAMMING IN PYTHON
TUTORIAL QUESTIONS
MODULE-III
1. Differentiate between terminal based and GUI based programming in Python?
ANS:
m
co
s.
te
no
la
ra
ke
2. Write a program to draw a hexagon using turtle. (university question)
Ans:
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
from turtle import Turtle
t=Turtle()
def hexagon(t, length):
for count in range(6):
[Link](length)
[Link](60)
hexagon(t,100)
m
3. Write a program to draw a pentagon using turtle.
co
Ans:
from turtle import Turtle
t=Turtle()
s.
te
def pentagon(t, length):
no
for count in range(5):
[Link](length)
la
[Link](72)
ra
pentagon(t,100)
ke
4. Write a program to draw a star using turtle.
Ans:
from turtle import Turtle
t=Turtle()
def star(t, length):
for count in range(5):
[Link](length)
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link](144)
star(t,100)
5. Write a program to draw a circle using turtle.
Ans:
from turtle import Turtle
t=Turtle()
r=100
[Link](r)
m
co
6. Write a GUI based program that allows the user to convert temperature values
s.
between degrees Fahrenheit and degrees Celsius. The interface should have
labeled entry fields for these two values. These components should arrange in a
te
grid where the labels occupy the first row and the corresponding field occupy
no
the second row. At start up the Fahrenheit field should contain 32 and the
Celsius field contain [Link] third row in the window contain two command
buttons ,labeled >>>> and <<<<.When the user presses the first button, the
la
program should use the data in the Fahrenheit field to compute the Celsius
ra
value, which should then be output to the Celsius field. The second button
should perform the inverse function? (university question)
ke
Ans:
from breezypythongui import EasyFrame
class NumberFieldDemo(EasyFrame):
def __init__(self):
EasyFrame.__init__(self, title = "Temperature Conversion")
[Link](text = "Celsius", row = 0, column = 0)
[Link] = [Link](value = 0, row = 0,column=1,
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
width = 10)
[Link]=[Link](text = "Fahrenheit",row = 1, column = 0)
[Link] = [Link](value = 0.0, row =1,column = 1,
width = 8, precision = 2)
# The command button
[Link](text = ">>>>", row = 2, column = 0, columnspan = 2,
command = [Link])
m
[Link](text = "<<<<", row = 2, column = 1,columnspan = 2,
co
command = [Link])
# The event handling method for the button
s.
def computefahr(self):
te
number = [Link]()
no
result = number * 1.8 + 32
la
[Link](result)
ra
def computecelsius(self):
ke
number = [Link]()
result = ((number-32)*5)/9
[Link](result)
NumberFieldDemo().mainloop()
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
7. Write a program to draw a radial pattern with 10 hexagons.
Ans:
from turtle import Turtle
t=Turtle()
def radialHexagons(t, n, length):
for count in range(n):
hexagon(t, length)
m
[Link](360 / n)
co
def hexagon(t, length):
for count in range(6): s.
[Link](length)
te
[Link](60)
no
radialHexagons(t,10,100)
la
ra
8. Write a GUI program to convert an input string to uppercase and displays the
result.
ke
Ans:
from breezypythongui import EasyFrame
class TextFieldDemo(EasyFrame):
def __init__(self):
EasyFrame.__init__(self, title = "Text Field Demo")
[Link](text = "Input", row = 0, column = 0)
[Link] = [Link](text = "",row=0,column = 1)
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link](text = "Output", row = 1, column = 0)
[Link]=[Link](text="",row=1,column=1)
[Link](text = "Convert", row=2,column= 0,columnspan
= 2, command = [Link])
# The event handling method for the button
def convert(self):
text = [Link]()
result = [Link]()
m
[Link](result)
co
TextFieldDemo().mainloop()
s.
te
no
la
ra
ke
9. Write GUI program to compute and displays the square root of an input
number.
Ans:
from breezypythongui import EasyFrame
import math
class NumberFieldDemo(EasyFrame):
def __init__(self):
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
EasyFrame.__init__(self, title = "Number Field Demo")
[Link](text = "An integer",row = 0, column = 0)
[Link] = [Link](value = 0,row = 0,column = 1,
width = 10)
[Link](text = "Square root",row = 1, column = 0)
[Link] = [Link](value = 0.0,row = 1,
column = 1,width = 8,precision = 2)
m
co
[Link](text = "Compute", row = 2, column = 0,
columnspan = 2, command = [Link])
s.
# The event handling method for the button
te
def computeSqrt(self):
no
number = [Link]()
la
result = [Link](number)
ra
[Link](result)
ke
NumberFieldDemo().mainloop()
[Link] a GUI program to inputs the integer, computes the square root, and
outputs the result and handles input errors by displaying a message box.
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
Ans:
from breezypythongui import EasyFrame
import math
class NumberFieldDemo(EasyFrame):
def __init__(self):
EasyFrame.__init__(self, title = "Number Field Demo")
m
[Link](text = "An integer",row = 0, column = 0)
co
[Link] = [Link](value = 0,row = 0,column = 1,
width = 10) s.
[Link](text = "Square root",row = 1, column = 0)
te
[Link] = [Link](value = 0.0,row = 1,
no
column = 1,width = 8,precision = 2)
la
ra
[Link](text = "Compute", row = 2, column = 0,
ke
columnspan = 2, command = [Link])
# The event handling method for the button
def computeSqrt(self):
try:
number = [Link]()
result = [Link](number)
[Link](result)
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
except ValueError:
[Link](title = "ERROR", message = "Input must
be an integer >= 0")
NumberFieldDemo().mainloop()
m
co
s.
te
no
[Link] a GUI-based program that plays a guess-the-number game in which the
user guesses a number between 1 and 100 and the computer provides the
la
responses. The window should display the user’s guesses with a label by
ra
saying, “Too large, try again,” or “Too small, try again.” When the user finally
guesses the correct number, the program congratulates him and tells him the
ke
total number of guesses.
import random
from breezypythongui import EasyFrame
class GuessingGame(EasyFrame):
def __init__(self):
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
"""Sets up the window, widgets, and data."""
EasyFrame.__init__(self, title = "Guessing Game")
# Initialize the instance variables for the data
[Link] = [Link](1, 100)
[Link] = 0
# Create and add widgets to the window
greeting = "Guess a number between 1 and 100."
m
[Link] = [Link](text = greeting,row = 0, column = 0,
co
sticky ="NSEW",columnspan = 2)
[Link](text = "Your guess", row = 1, column = 0)
s.
[Link] = [Link](0, row = 1, column = 1)
te
# Buttons have no command attributes yet
no
[Link] = [Link](text = "Next", row = 2,
la
column = 0,command=[Link])
ra
[Link] = [Link](text = "New game",row = 2,
ke
column = 1,command=[Link])
def nextGuess(self):
"""Processes the user's next guess."""
[Link] += 1
guess = [Link]()
if guess == [Link]:
[Link]["text"] = "You've guessed it in " +str([Link])+"attempts!"
10
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link]["state"] = "disabled"
elif guess < [Link]:
[Link]["text"] = "Sorry, too small!"
else:
[Link]["text"] = "Sorry, too large!"
def newGame(self):
"""Resets the data and GUI to their original states."""
m
[Link] = [Link](1, 100)
co
[Link] = 0
greeting = "Guess a number between 1 and 100."
s.
[Link]["text"] = greeting
te
[Link](0)
no
[Link]["state"] = "normal"
la
GuessingGame().mainloop()
ra
ke
[Link] a GUI-based program that plays a guess-the-number game in which the
computer guesses a number between 1 and 100 and the user provides the
responses. The window should display the computer’s guesses with a label. The
user enters a hint in response, by selecting one of a set of command buttons
labeled Too small, Too large, and Correct. When the game is over, you should
11
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
disable these buttons and wait for the user to click New game, as before.
(university question)
Ans:
import random
from breezypythongui import EasyFrame
class GuessingGame(EasyFrame):
def __init__(self):
m
EasyFrame.__init__(self, title = "Guessing Game")
co
[Link] = [Link](1, 100)
[Link] = 0
s.
te
[Link] = [Link](text = "",row = 0, column = 0,
no
sticky = "NSEW",columnspan = 2)
[Link](text = "Computer guess", row = 1, column = 0)
la
ra
[Link] = [Link](0, row = 1, column = 1)
[Link]([Link])
ke
[Link] = [Link](text = "Too small", row = 2,
column =0,command=[Link])
[Link] = [Link](text = "Too large",row = 2,
column = 1,command=[Link])
[Link] = [Link](text = "Correct", row = 2,
column = 2,command=[Link])
12
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link] = [Link](text = "New Game", row = 2,
column = 3,command=[Link])
def Guess(self):
[Link] += 1
Response=30
if Response == [Link]:
[Link]["text"] = "You've guessed it in " +str([Link]) +
m
" attempts!"
co
[Link]["state"] = "disabled"
[Link]["state"] = "disabled"
s.
[Link]["state"] = "disabled"
te
elif Response < [Link]:
no
[Link] = [Link](1, 100)
la
[Link]([Link])
ra
else:
ke
[Link] = [Link](1, 100)
[Link]([Link])
def newGame(self):
"""Resets the data and GUI to their original states."""
[Link] = [Link](1, 100)
[Link] = 0
greeting = ""
13
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link]["text"] = greeting
[Link]([Link])
[Link]["state"] = "normal"
[Link]["state"] = "normal"
[Link]["state"] = "normal"
GuessingGame().mainloop()
m
co
s.
te
no
la
ra
ke
14
For More Study Materials : [Link]