Skip to content

Instantly share code, notes, and snippets.

@Mr-Coxall
Last active July 1, 2018 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mr-Coxall/560f860560c48bddbfe17f25f85d78f2 to your computer and use it in GitHub Desktop.
Save Mr-Coxall/560f860560c48bddbfe17f25f85d78f2 to your computer and use it in GitHub Desktop.
area_and_perimeter_ver2.py
# Created by: Mr. Coxall
# Created on: Aug 2018
# Created for: ICS3U
# This program displays area and perimeter of a rectangle,
# but this time the user can enter different lengths and widths
import ui
def calculate_button_touch_up_inside(sender):
# calculate area and perimeter
# input
length = int(view['length_textbox'].text)
width = int(view['width_textbox'].text)
# process
area = length * width
perimeter = 2 * (length + width)
# output
view['area_answer_label'].text = 'The area is: ' + str(area) + ' cm^2'
view['perimeter_answer_label'].text = 'The perimeter is: ' + str(perimeter) + ' cm'
view = ui.load_view()
view.present('full_screen')
[
{
"selected" : false,
"frame" : "{{0, 0}, {1024, 768}}",
"class" : "View",
"nodes" : [
{
"selected" : false,
"frame" : "{{47, 48}, {150, 32}}",
"class" : "Label",
"nodes" : [
],
"attributes" : {
"font_size" : 18,
"frame" : "{{437, 368}, {150, 32}}",
"uuid" : "9D71BC78-7DDF-4469-9177-5B168C9E3416",
"class" : "Label",
"alignment" : "left",
"text" : "Length",
"name" : "length_label",
"font_name" : "<System>"
}
},
{
"selected" : false,
"frame" : "{{47, 112}, {150, 32}}",
"class" : "Label",
"nodes" : [
],
"attributes" : {
"font_size" : 18,
"frame" : "{{437, 368}, {150, 32}}",
"uuid" : "43FD99B1-4C50-4BA5-8F04-90C2DD26CBEC",
"class" : "Label",
"alignment" : "left",
"text" : "Width",
"name" : "width_label",
"font_name" : "<System>"
}
},
{
"selected" : false,
"frame" : "{{221, 48}, {200, 32}}",
"class" : "TextField",
"nodes" : [
],
"attributes" : {
"font_size" : 17,
"frame" : "{{412, 368}, {200, 32}}",
"spellchecking_type" : "default",
"class" : "TextField",
"uuid" : "3F6E5C78-F9FF-450B-8A35-3584944A73BE",
"alignment" : "left",
"autocorrection_type" : "default",
"name" : "length_textbox",
"font_name" : "<System>"
}
},
{
"selected" : false,
"frame" : "{{221, 112}, {200, 32}}",
"class" : "TextField",
"nodes" : [
],
"attributes" : {
"font_size" : 17,
"frame" : "{{412, 368}, {200, 32}}",
"spellchecking_type" : "default",
"class" : "TextField",
"uuid" : "AB297317-F7FC-4A8E-ABBF-6B2CD207028D",
"alignment" : "left",
"autocorrection_type" : "default",
"name" : "width_textbox",
"font_name" : "<System>"
}
},
{
"selected" : true,
"frame" : "{{47, 177}, {80, 32}}",
"class" : "Button",
"nodes" : [
],
"attributes" : {
"action" : "calculate_button_touch_up_inside",
"frame" : "{{472, 368}, {80, 32}}",
"title" : "Calculate",
"uuid" : "FB1B21EA-93D8-4EAF-B493-D1088DCBAD4F",
"class" : "Button",
"name" : "calculate_button",
"font_size" : 15
}
},
{
"selected" : false,
"frame" : "{{47, 225}, {374, 32}}",
"class" : "Label",
"nodes" : [
],
"attributes" : {
"font_size" : 18,
"frame" : "{{437, 368}, {150, 32}}",
"uuid" : "3E92C9CD-8536-4F50-AE26-505B9EFA524D",
"class" : "Label",
"alignment" : "left",
"text" : "The perimeter is:",
"name" : "perimeter_answer_label",
"font_name" : "<System>"
}
},
{
"selected" : false,
"frame" : "{{47, 274}, {374, 32}}",
"class" : "Label",
"nodes" : [
],
"attributes" : {
"font_name" : "<System>",
"frame" : "{{437, 368}, {150, 32}}",
"uuid" : "2E58034F-1AC5-4E70-9936-8145C467DBFC",
"class" : "Label",
"alignment" : "left",
"text" : "The area is:",
"name" : "area_answer_label",
"font_size" : 18
}
}
],
"attributes" : {
"enabled" : true,
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment