[Tutor] Problem with "input" in Python 3
peterjohnanderson at gmail.com
Tue Feb 16 00:09:35 CET 2010
Sorry about the previous post; I didn't check the code format before I
posted the reply and thus lost all the indentation. Here I go again,
hopefully with the correct indentation.
# 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
from easygui import *
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
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)
b = int(fieldValues)
c = int(fieldValues)
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)
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/,
More information about the Tutor