python


Why does the following code not follow the specified order?


I want it to say welcome, ask for the user input (a,b,c), validate the user input and if the validation returns that the input is reasonable then carry out the quadratic formula on a,b,c. I suspect the problem is in the while-loop. The program just welcomes, asks for input then says welcome again and so on.
from math import sqrt
def quadratic_formula(a,b,c):
a=float(a) #The quadratic formula
b=float(b)
c=float(c)
x1_numerator = -1*b + sqrt((b**2)-4*(a*c))
x2_numerator = -1*b - sqrt((b**2)-4*(a*c))
denominator = 2*a
x1_solution = x1_numerator/denominator
x2_solution = x2_numerator/denominator
print("x= "+str(x1_solution)+" , x= "+str(x2_solution))
def number_check(a,b,c,check): #carries out a check
a=float(a)
b=float(b)
c=float(c)
if (b**2)-4*a*c < 0:
print("The values you have entered result in a complex solution. Please check your input.")
check == False
else:
check == True
check = False
while check == False:
print("Welcome to the Quadratic Equation Calculator!")
a = input("Please enter the x^2 coefficient: ")
b = input("Please enter the x coefficient: ")
c = input("Please enter the constant: ")
number_check(a,b,c,check)
else:
quadratic_formula(a,b,c)
You are correct in your suspicion. You have a problem in your while loop. does not work the way your code assumes.
Instead you need to write something like:
def number_check(a,b,c): #carries out a check
a=float(a)
b=float(b)
c=float(c)
if (b**2)-4*a*c < 0:
print("The values you have entered result in a complex solution. Please check your input.")
check = False
else:
check = True
return check
check = False
print("Welcome to the Quadratic Equation Calculator!")
while check == False:
a = input("Please enter the x^2 coefficient: ")
b = input("Please enter the x coefficient: ")
c = input("Please enter the constant: ")
check = number_check(a,b,c)
quadratic_formula(a,b,c)
Note, that in addition to changing the while loop you also need to update number_check as input parameters are not updated in calling scope. Instead the function has to explicitly return the updated value.
Try using return, not attempting to modify a global variable.
There's a way to use global variables (see global statement), but it's not necessary for this code.
The check variable itself isn't really necessary, though
def number_check(a,b,c):
a=float(a)
b=float(b)
c=float(c)
return (b**2)-4*a*c >= 0 # return the check
while True:
print("Welcome to the Quadratic Equation Calculator!")
a = input("Please enter the x^2 coefficient: ")
b = input("Please enter the x coefficient: ")
c = input("Please enter the constant: ")
if not number_check(a,b,c):
print("The values you have entered result in a complex solution. Please check your input.")
else:
break # getting out of the loop
There are two problems with the way you're using the check variable in the number_check function.
First, you're not assigning new values to it, because you're using == (which tests equality) rather than =.
But also, since it's a parameter variable, it's local to the function. So assigning it inside the function does not modify the global variable that you test in the while loop. Rather than use a global variable, you can simply test the result of number_check directly, and use break when you want to end the loop.
If you make this change, you need to move the call to quadratic_formula out of the else: clause, because that's only executed when the while condition fails, not when we end the loop with break.
def number_check(a,b,c): #carries out a check
a=float(a)
b=float(b)
c=float(c)
if (b**2)-4*a*c < 0:
print("The values you have entered result in a complex solution. Please check your input.")
return False
else:
return True
while True:
print("Welcome to the Quadratic Equation Calculator!")
a = input("Please enter the x^2 coefficient: ")
b = input("Please enter the x coefficient: ")
c = input("Please enter the constant: ")
if number_check(a,b,c):
break
quadratic_formula(a,b,c)

Related Links

How to make random algorithm more efficient
How to force google to allow user to choose accounts during oauth?
PyQt: Qtablewidget itemClick
Pandas column indexing for searching?
Buildozer/Kivy: TextInput crashes on numeric input in Android
Adding markers or lines to colorbar in matplotlib
Code against Pywin32 on OS X?
Pandas: Find previous row of matching value
PyGtk3, ToggleButton, “toggled-or-untoggled” event
Reaplce img value with value from list Python
Python - Displaying json data
Partial patch in Python with mock
Radio button does not get clicked in Selenium / Python
How to get base url only from textfile in python?
Processing Multiple files in hadoop python
GAE Python: Control access based on GSuite group

Categories

HOME
redis
oauth
smartphone
adsense
java-home
bro
spring-cloud-contract
google-plus
dropbear
keystore
outlook-web-addins
google-shopping
finite-group-theory
informatica-powercenter
ctypes
cratedb
symfony2-easyadmin
rebol2
scaling
pyyaml
factor-analysis
ada
aptana
internet-explorer-8
attask
sql-update
cruisecontrol.net
countif
avplayeritem
skmaps
osmdroid
mangodb
pep8-assembly
vegan
crt
pari
getline
google-drive-realtime-api
recurrence-relation
service-locator
openpgp
nunit-3.0
xen
bigcartel
rhel.net
strstr
median
lirc
goquery
spell-checking
maintenance
ruby-on-rails-2
visual-studio-templates
gradle-script-kotlin
vst
easing
unsatisfiedlinkerror
gherkin
usb-drive
flexjson
multinomial
difference
self-hosting
plone-3.x
windowlistener
qbfc
programming-paradigms
prezto
debian-based
uos
tilestache
ember-components
dto
author
colt
lov
real-time-updates
driver-signing
ftp4j
vmware-server
mstsc
dwolla
qss
phpsh
maven-ear-plugin
die
flash-cs5.5
querystringparameter
ou
drawtext
hibernate3-maven-plugin
quick-search
revert
mvvm-foundation
revision
soft-keyboard

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App