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

Feed google charts custom properties like color through gviz_api
combining python with fortran, trouble with tutorial
unable to deploy portia spider with scrapyd-deploy
How do I get a function inside of a while loop in a function to work properly?
Celery / RabbitMQ / Django not running tasks
Python creating a while loop depending on input [duplicate]
django model default=“” and “This field cannot be blank”
Scrapy - Importing Excel .csv as start_url
Make a simple audio player with a playback control with gstreamer1.0 and gtk3
error in writing a text file in python [closed]
ipython using 2.6 version instead of 2.7
Django: How to override authenticate() method?
Execute python script with a variable from linux shell
Output loop result into a list
Pyinstaller will create an executable with --onefile, but not without it
PyMongo update multiple records with multiple data

Categories

HOME
checkbox
google-tag-manager
facebook-graph-api
google-contacts
computer-vision
survival-analysis
couchdb-2.0
orientation
algorithmic-trading
dropbear
unity2d
hosts
ssms-2016
dendrogram
bootstrap-popover
arraylist
grub2
marathon
raphael
apple-tv
countif
myob
pdfsharp
branch
image-quality
xor
piecewise
backup-strategies
idl
spreedly
swiftcharts
geomesa
swagger-editor
meta-raspberrypi
mongoexport
duktape
ntfs-mft
fax
xbim
react-native-router-flux
google-account
apiary
lirc
floor
taskmanager
spell-checking
udev
instruction-set
gce
autoresize
hspec
spark-cassandra-connector
multilingual-app-toolkit
gradle-release-plugin
qt-linguist
yoothemes
asp.net-mvc-2
cmocka
registrykey
fabric-twitter
python-winshell
anti-patterns
twython
rx-groovy
ibm-data-studio
rspec3
google-plus-one
hana-xs
jcr-sql2
iostream
mov
genetic-programming
alertifyjs
joox
json-patch
db4o
astyanax
entity-framework-4.1
resgen
qss
tridion2009
hobo
cross-domain-policy
tcxgrid
newsstand-kit
gethashcode
swfloader
weborb
ajaxpro
hardware-infrastructure
document-library
cots





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