Anybody help with a bit of C++ to Python please?
Discussion
Put an Else at the end?
https://stackoverflow.com/questions/3295938/else-c...
Not pretty.... but if it works.
https://stackoverflow.com/questions/3295938/else-c...
Not pretty.... but if it works.
Sadly it didn't work for me.
I just got a Python error.
Should anyone want to compile this in c++ this is the full code (tried and tested) (unless someone can show me how to write this to the serial port on Linux and then read back the resulting output in real time...
int main() {
for (float freq = 15.0; freq < 20000.0; freq = freq + (freq / 100) * 12.2462) {
cout << freq << "\n";
}
return 0;
}
Can compile with 'g++ -o looptest test.cpp'
Output should be:
15
16.8369
18.8988
21.2132
23.811
26.727
30
33.6739
37.7976
42.4264
47.622
53.4539
60
67.3477
75.5952
84.8528
95.244
106.908
120
134.695
151.19
169.705
190.488
213.815
240
269.391
302.381
339.411
380.976
427.631
479.999
538.781
604.761
678.822
761.952
855.262
959.999
1077.56
1209.52
1357.64
1523.9
1710.52
1920
2155.12
2419.04
2715.29
3047.8
3421.04
3839.99
4310.25
4838.09
5430.57
6095.61
6842.09
7679.98
8620.49
9676.17
10861.1
12191.2
13684.2
15360
17241
19352.3
I just got a Python error.
Should anyone want to compile this in c++ this is the full code (tried and tested) (unless someone can show me how to write this to the serial port on Linux and then read back the resulting output in real time...

- include <string>
- include <iostream>
int main() {
for (float freq = 15.0; freq < 20000.0; freq = freq + (freq / 100) * 12.2462) {
cout << freq << "\n";
}
return 0;
}
Can compile with 'g++ -o looptest test.cpp'
Output should be:
15
16.8369
18.8988
21.2132
23.811
26.727
30
33.6739
37.7976
42.4264
47.622
53.4539
60
67.3477
75.5952
84.8528
95.244
106.908
120
134.695
151.19
169.705
190.488
213.815
240
269.391
302.381
339.411
380.976
427.631
479.999
538.781
604.761
678.822
761.952
855.262
959.999
1077.56
1209.52
1357.64
1523.9
1710.52
1920
2155.12
2419.04
2715.29
3047.8
3421.04
3839.99
4310.25
4838.09
5430.57
6095.61
6842.09
7679.98
8620.49
9676.17
10861.1
12191.2
13684.2
15360
17241
19352.3
I see you're on Windows - I also tried Python3 on Linux to no avail.
This however works.
Thanks all.
ETA: I even tried this with /usr/bin/python3.4 to no avail - but I note PlasticPig has Python 3.6 - the highest I have is 3.4 - I guess it must be that?
This however works.
#!/usr/bin/python
def my_range(start, end):
while start <= end:
yield start
start += (start / 100) * 12.2462
for x in my_range(15.0, 20000.0):
print(x)
Thanks all.
ETA: I even tried this with /usr/bin/python3.4 to no avail - but I note PlasticPig has Python 3.6 - the highest I have is 3.4 - I guess it must be that?
Edited by TonyRPH on Thursday 4th January 19:58
TonyRPH said:
I see you're on Windows - I also tried Python3 on Linux to no avail.
This however works.
Thanks all.
Worked on Linux for me as well:This however works.
#!/usr/bin/python
def my_range(start, end):
while start <= end:
yield start
start += (start / 100) * 12.2462
for x in my_range(15.0, 20000.0):
print(x)
Thanks all.
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
>
15.0
16.83693
18.89881412166
21.213200696626725
23.811011680337028
26.72695579273446
29.99999225302431
33.67385130431417
37.79761848274309
42.426390437376774
47.62201106311881
53.453897781930465
59.99996901210123
67.34768521726117
75.59521744433741
84.85275896300585
95.24399753113347
106.90776795679113
119.99990703631569
134.695335651797
151.19039584638736
169.70547410252766
190.4879458720714
213.81548069945703
239.99975209687392
269.3906017381613
302.38071360822005
339.4108605581099
380.97579336377714
427.630850970692
479.99938024226486
538.7810643454931
604.7612710473709
678.821545822374
761.9513899668735
855.2614810849968
959.9985125816277
1077.5618504293989
1209.5222297566838
1357.6427410571468
1523.9023864124872
1710.5225204573333
1919.9965293575792
>
TonyRPH said:
How odd.
But yet this doesn't run.
Did I do something wrong?
Yes!But yet this doesn't run.
Did I do something wrong?
Edited by TonyRPH on Thursday 4th January 20:02
#!/usr/bin/python3.4
freq = 15
while freq < 20000:
print(freq)
freq = freq + (freq / 100) * 12.2462
Note that the "freq =" is indented the same as the print function. Indentation is important in Python as it marks blocks of code. I should have used code tags on my original post to keep the indentation....
Edited by plasticpig on Thursday 4th January 20:10
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


