Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ to Python conversion
#1
Hello,

I wrote a programme a while back in C++ whilst at college, i am now trying to re-create it in python.

The project was a musical door bell that used 8 relays to hit bells, i am trying to make this work with a rasberry pi and its GPIO pins.

The structure of the C++ code was a loop that would run twice and would call up one of four labeled 'tunes' at random (the button was pushed and the bells would ring two random tunes of the four written in).

This used the 'goto' command and labels for each tune..... from what i can work out Python doesnt like that or work that way.

What is the easiest way to create the same kind of thing? i have the four tunes written and working, but no idea how to seperate them or to get the program to select at random........ (this is me trying to learn python so be kind)

Any help or direction would be appreciated.

Gill
Reply
#2
Firstly, welcome to Python and welcome to the forum.

Posting the Python code you've tried as well as any error codes (the full error code please) would be helpful.  Also, explaining the output you are getting versus what you are expecting would also be helpful.  Be sure to post any code and errors between the proper 'code tags'.  Refer to the Help Document on how to do this properly.

To get you started,
Quote:... or to get the program to select at random

you will want to look into the Random library.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Hello,

I dont actually have any code yet other than the 4 tunes. I was trying to work out how to go about writing what i needed to.

Thank you for the Random library i will have a look into that Smile .
Reply
#4
If you are trying to get around the lack of goto and labels, I would look at using functions. Have a function for each tune, and call the functions at random. Better yet, have a function that can play any one of the tunes based on input provided to it, and call that function with random values.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Before continuing, you said this is for a Raspberry Pi, what OS are you using? Raspbian or some other OS.  Do you have access to Python 3.x or only Python 2.x?

I've not dealt with the Raspberry Pi, but do you 'talk' to the switches in a binary manner? For example: relay 1 = 00000001, relay 2 = 00000010 ....?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
for those of us who use c++, it would be helpful to post that code.
goto labels in c++ ... Ouch
Reply
#7
Hello again,

I have managed to work out the randomising and how to define each 'tune' but i cant get my loop to work properly, ity doesnt stop and runs forever.

As the code is on my Pi (which had NOOB on it when i got it and i am writting code in Python 3) i will write a short version of it below, with out the pretty colours.


import random

def R(): (these are the tunes)
def Q():
def RQ():
def T():

List = [R,Q,RQ,T]
count = 0

while count < 2:
random.choice(List)()
count + 1

if count > 2:

break
As i say, it runs and randomises amazingly..... it just doesnt stop, do i need to import something at the begining to make it count? or have i just been a muppet a done something blatantly stupid?

Thank you for the help


Its ok i missed the = out of the count + line..... i think im starting to understand this stuff :) :)
Reply
#8
Not a good idea to name lists List
The break after is count > 2 is not part of the if statement
(these are the tunes) -- This is not a comment
comments have # to start
the functions R, Q, RQ and T have no body - they do nothing

This code will not run as presented
line 3 will give a syntax error

clean up your code, test and re-post

wlhile loop has no indentation.
Reply
#9
I agree with Lars60+.  I don't see how it can 'run and randomizes amazingly'.  Here is an example of runnable code:

import random

""" This is
a multiline comment """

# This is a single line comment

R, Q, RQ, T = "A", "B", "C", "D"    # Assign some kind of value to R, Q, RQ, T. Can be anything i.e. integers, floats, strings
tunes = [R, Q, RQ, T]    # Add variables to list
count = 0    # Initialize count to 0

while True:
    if count <2:    # Gives two selections, remember, 'count' comes in as zero
        print("\nEntry count ", count)
        print(random.choice(tunes))    # For testing, print what 'random is choosing
        count += 1
        print("Exit count ", count)    # For testing, print value of count
    else:
        print("\nThe 'while' condition is now False, exiting the loop")
        break
Note: tunes could also be a dictionary, hard to tell with out knowing what they do (maybe correspond to the data pins on the GPIO?)

At some point, you may also want to check out Python Pigpio.

Do not be afraid to use 'print()', at least during your testing phase, it's cheap and easily removed when everything works as intended  Smile
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#10
Sorry, the code i put up was just an idea of what i was doing not the actual code.


This is what i have so far. However i am getting  'RuntimeWarning: this channel is already in use, continuing anyway.' when the code is run.
If you take out all the GPIO stuff it runs fine, and i have written the GPIO 'tunes' in seperate file, shich also runs fine, however the relays arent triggering when it is all put together like this, any ideas??

Id also just like to say a massive thank you for helping in my muppetry Big Grin


Moderator sparkz_alot:
Please put code, output and errors between the corresponding code tags.  For assistance, please consult the Help Document
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I need to add data types to cython conversion python to c Good_AI_User 1 1,011 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 2 6,515 Jul-09-2020, 04:46 PM
Last Post: srikanth7482
  C to Python code conversion print problem anakk1n 1 2,191 May-22-2020, 04:15 PM
Last Post: deanhystad
  Python uppercase conversion conditions Jaypeng 7 2,994 Apr-29-2020, 11:24 AM
Last Post: jefsummers
  MATLAB to Python conversion stokd 10 5,040 Jan-19-2020, 09:14 PM
Last Post: stokd
  Excel Model Conversion into Python line7 2 2,274 Oct-09-2019, 07:51 AM
Last Post: line7
  python opencv grayscale conversion error Spandora 1 9,582 May-26-2019, 10:43 AM
Last Post: heiner55
  Vba conversion to Python stranger14u 1 3,594 May-26-2019, 08:01 AM
Last Post: heiner55
  Conversion of Time Duration in seconds in python jdevansh99 0 2,863 Jun-05-2018, 05:12 PM
Last Post: jdevansh99

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020