[Tutor] Problem with "input" in Python 3
pja
peterjohnanderson at gmail.com
Tue Feb 16 00:02:12 CET 2010
Here is an interesting solution that I developed using the EasyGUI
(http://easygui.sourceforge.net/) graphics library:
# 7.2_quadratic2easy.py
# A program that computes the real roots of a quadratic equation.
# Bad version using a simple if to avoid program crash
# This version uses the EasyGUI graphics library
# See http://easygui.sourceforge.net/ for EasyGUI details
import math
from easygui import *
def main():
msg = "This program finds the real solutions to a quadratic" + "\n" + \
"Please enter the three coefficients below."
title = "quadratic2easy.py"
fieldNames = ["First coefficient","Second coefficient","Third coefficient"]
fieldValues = [] # we start with blanks for the values
fieldValues = multenterbox(msg,title, fieldNames)
# make sure that none of the fields was left blank
while 1:
if fieldValues == None: break
errmsg = ""
for i in range(len(fieldNames)):
if fieldValues[i].strip() == "":
errmsg = errmsg + ('"%s" is a required field.' % fieldNames[i])
if errmsg == "": break # no problems found
fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues)
print("Reply was:", fieldValues)
a = int(fieldValues[0])
b = int(fieldValues[1])
c = int(fieldValues[2])
discrim = a * a - 4 * b * c
if discrim >= 0:
discRoot = math.sqrt(discrim)
root1 = (-a + discRoot) / (2 * b)
root2 = (-a - discRoot) / (2 * b)
message = "The real solutions to the quadratic equation are " +
str(root1) + " and " + str(root2)
msgbox(msg=message, title="quadratic2easy.py",
ok_button="OK", image=None)
main()
Thanks to all those who provided advice; this is a great resource for us
learners.
Regards,
Peter
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to
conduct, or more uncertain in its success, than to take the lead in the
introduction of a new order of things—Niccolo Machiavelli, /The Prince/,
ch. 6
More information about the Tutor
mailing list