Here is the section of the code that I'm having trouble with:
def gbp():
gbpTo = int(input("Which currency are you converting into?"))
if gbpTo == "1":
print("You are converting Pounds Sterling to Pounds Sterling... there is no conversion needed!")
elif gpbTo == "2":
num = float(input("Please type in the amount of Pounds Sterling that you wish to convert into US Dollars")
calc = num * 1.55
calc = round(calc, 2)
print(num + " Pounds Sterling in US Dollars is $", calc)
When I run it, it comes up with syntax error, highlighting calc. What should I do?
One parenthesis is missing, the one that closes float().
...
num = float(input("Please type in the amount of Pounds Sterling that you wish to convert into US Dollars"))
...
Related
I want to get length scale, length unit, energy scale, and energy unit from an EELS map data by DM script
source.ImageGetDimensionCalibration(0, sOrig, sScale, sUnit, 0)
source.ImageGetDimensionCalibration(2, eOrig, eScale, eUnit, 0)
The above codes did not work, it always make the energy unit with 1eV
For example, an EELS data with dispersion 0.9 ev/pixel, or 0.5 ev/pixel, and the energy range from 0 to 400, and the EELS map is 10nm x8nm, I need to get all this information with DM script.
Now the bug of my code is it always count the disepersion is 1 ev/pixel
Any suggestions, thanks
The following script will hopefully clear up any questions:
void PrintCalibrations(image source)
{
number nDim = source.ImageGetNumDimensions()
result("\n '[" + source.ImageGetLabel() +"]:"+ source.ImageGetName() +"' calibrations:")
result("\n Data has " + nDim + " dimensions.")
for( number d=0; d<nDim; d++ )
{
string unit_dim
number scale_dim, origin_dim
source.ImageGetDimensionCalibration(d, origin_dim, scale_dim, unit_dim, 0)
result("\n\t Dimension #"+d+": scale = " + scale_dim + " \t origin = " + origin_dim + " \t [" + unit_dim + "]" )
}
}
// Pick your data by image letter!
Image SIdata_3D := B
Image PickerSpectrum_1D := A
Image elementalMap_2D := D
ClearResults()
PickerSpectrum_1D.PrintCalibrations()
elementalMap_2D.PrintCalibrations()
SIdata_3D.PrintCalibrations()
Applied to a test data set:
I was asked to calculate the Pi number using the Leibniz formula for Pi with a given accuracy (eps).
The formula looks like this:
Initially, I wrote the following code:
fun main() {
val eps = 0.005
var n = 2
var r = row(n) // current row
var r0 = row(n-1)
var s = r0 + r
while (Math.abs(r) > eps) {
n++
r = row(n)
s += r
}
println(r.toString() + " <-- Leibniz(" + n.toString() + ")")
println(Math.abs(s*4).toString() + " <-- our evaluation with eps")
println(Math.PI.toString() + " <-- real Pi")
println((Math.abs(s*4)) in (Math.PI-eps..Math.PI+eps))
}
fun row(n: Int) = ((Math.pow(-1.0, n.toDouble()))/(2*n-1))
Then I found out that it doesn't work correctly, because
println((Math.abs(s*4)) in (Math.PI-eps..Math.PI+eps)) printed false.
I went deeper, made a debug, and realised that if went with
while (Math.abs(r) > eps/2)
over
while (Math.abs(r) > eps) everything works fine.
Could someone please provide any explanation on what I did wrong or why I have to divide eps by 2 if that is correct.
Thanks.
Each term r_i in that series is summed up to PI with a factor of 4 because sum(r_0, .., r_n) = PI/4. So of course, when you stop at the first r_i <= eps that only means that sum(r_0, ..., r_(i-1)) has an accuray of eps, ie it is somewhere in between [PI/4 - eps/2, PI/4 + eps/2]. But PI it self is 4*sum thus the accuracy is of course 4*eps ie the approximation lies somewhere inbetween [PI-2*eps ,PI+2*eps]
For your value of eps = 0.005:
The first r_100 = 0.00497512... is the first r <= eps
sum(r0, ..., r_99) = 0.782829, so PI at that point would be approximated as 3.1315929
EDIT
Also you are actually calculating -PI because are flipping the sign of each term in the series. So what you call r0 in your code (it should rather be called r1 because it's the result of row(1)) is -1 instead of +1
When you check Math.abs(r) > eps you're looking at the size of the n-th element of the series.
The distance of your current approximation from PI is the sum of all the terms in the series after that one.
As far as I know the relationship between the size of the n-th element of a convergent series and how good of an approximation you have depends on the specific series you are summing.
On a webservice program,i fetched temperature value from an API and its in kelvin value....i want to convert that to celsius and print to another label in same page along with that kevin value... As a beginner i don't know much how to do..please help
lbl1.text=[dic retrieveForPath:#"current.city.#name"];
lbl2.text=[dic retrieveForPath:#"current.city.coord.#lon"];
lbl3.text=[dic retrieveForPath:#"current.temperature.#value"];
here on 'lbl3 ,i printed temperature in kelvin.....and how to convert and print that to another label by doing this equation T(°C) = T(K) - 273.15
Assuming that you have a lbl4 object, this will display the data with 2 decimal places, e.g. 12.51C
float centigrade = [[dic retrieveForPath:#"current.temperature.#value"] floatValue] - 273.15;
lbl4.text = [NSString stringWithFormat:#"%.2fC", centigrade];
I have a set of household GPS coordinates data, and the format in excel sheet looks like the following (edited for confidential reason):
ID GPSN GPSS GPSE GPSW
1 211234 -9 890123 -9
2 211255 -9 890155 -9
...
My questions are: what kind of GPS coordinates this is (looks like UTM data)? How do I accurately convert them into decimal degrees that only containing a longitude and a latitude (or X, Y data)? Do I need some kind of zoning information to do this correctly? Thanks
I doubt that a GPS receiver would put out UTM coordinates. It looks to me like latitude and longitude in degrees/minutes/seconds (DDMMSS). If so, then one way to do it is the following, in simple Python. The Convert Coordinate Notation tool in ArcGIS might be useful, but you'll have to reformat the data first, probably using Python.
import csv
import sys
# A function that takes 211234, treats it as 21°12'34",
# and returns 21.209444.
def convertToDegrees(DMS):
dms = DMS
dms = int(dms)
seconds = dms % 100
if 60 <= seconds:
print "More than 60 seconds! " + str(DMS) + " is not degrees/minutes/seconds!"
dms /= 100
minutes = dms % 100
if 60 <= minutes:
print "More than 60 minutes! " + str(DMS) + " is not degrees/minutes/seconds!"
dms -= minutes
degrees = dms / 100
degrees += (minutes / 60.0)
degrees += (seconds / (60.0 * 60.0))
if 180 < degrees or -180 > degrees:
print "In " + str(DMS) + ", degrees is outside [-180, 180]: " + str(degrees)
return degrees
# Input and output files from command line parameters
inFilename = sys.argv[1]
outFilename = sys.argv[2]
readFirstRow = False
with open(inFilename, "rb") as inFile:
reader = csv.reader(inFile)
with open(outFilename, "wb") as outFile:
writer = csv.writer(outFile)
# Loop through the rows
for row in reader:
if (not readFirstRow):
# Write the header row only once
writer.writerow(["ID", "latitude", "longitude"])
readFirstRow = True
else:
# Convert this row to latitude and longitude
latitude = 0
longitude = 0
if "-9" != row[1]:
latitude = convertToDegrees(row[1])
if "-9" != row[2]:
latitude = -1 * convertToDegrees(row[2])
if "-9" != row[3]:
longitude = convertToDegrees(row[3])
if "-9" != row[4]:
longitude = -1 * convertToDegrees(row[4])
writer.writerow([row[0], latitude, longitude])
To make sure you get it right, you'll want to confirm that the GPS was putting out latitude and longitude and find out which datum it used (probably WGS 1984).
I have tried to write a function that takes in notes in MIDI form (C2,A4,Bb6) and returns their respective frequencies in hertz. I'm not sure what the best method of doing this should be. I am torn between two approaches. 1) a list based one where I can switch on an input and return hard-coded frequency values given that I may only have to do this for 88 notes (in the grand piano case). 2) a simple mathematical approach however my math skills are a limitation as well as converting the input string into a numerical value. Ultimately I've been working on this for a while and could use some direction.
You can use a function based on this formula:
The basic formula for the frequencies of the notes of the equal
tempered scale is given by
fn = f0 * (a)n
where
f0 = the frequency of one fixed note which must be defined. A common choice is setting the A above middle C (A4) at f0 = 440 Hz.
n = the number of half steps away from the fixed note you are. If you are at a higher note, n is positive. If you are on a lower note, n is negative.
fn = the frequency of the note n half steps away. a = (2)1/12 = the twelth root of 2 = the number which when multiplied by itself 12 times equals 2 = 1.059463094359...
http://www.phy.mtu.edu/~suits/NoteFreqCalcs.html
In Objective-C, this would be:
+ (double)frequencyForNote:(Note)note withModifier:(Modifier)modifier inOctave:(int)octave {
int halfStepsFromA4 = note - A;
halfStepsFromA4 += 12 * (octave - 4);
halfStepsFromA4 += modifier;
double frequencyOfA4 = 440.0;
double a = 1.059463094359;
return frequencyOfA4 * pow(a, halfStepsFromA4);
}
With the following enums defined:
typedef enum : int {
C = 0,
D = 2,
E = 4,
F = 5,
G = 7,
A = 9,
B = 11,
} Note;
typedef enum : int {
None = 0,
Sharp = 1,
Flat = -1,
} Modifier;
https://gist.github.com/NickEntin/32c37e3d31724b229696
Why don't you use a MIDI pitch?
where f is the frequency, and d the MIDI data.