Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Please need help with converting c++ code to python



  • Hello, i have one work, that i must to complete in my colledge. We are working only on c++, but without any experience we must rewrite this code to pyton and i don't know how that's possible. Who can help me with this problem. I will be very happy and grateful if someone can convert this code:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
            cout << "Noteikt skaitlja m pirmo n ciparu summu\n";
            int n, m, sum=0;
    
            cout << "Ievadiet m un n veselos skaitlus\n";
            cin >> m >> n; 
    
            int mKopija = m, x=1;
    	    while ((mKopija/=10) > 0) x++;
    
    	    int c = x - n; 
                if (c < 0)
                    cout << "Nepietiek ciparu skaitlii" << endl; 
                else{
                    for(int i=1; i<=c; i++) { 
    
                        m /= 10;
    
                    }
                while (m > 0) {
    
                    sum += m%10; 
                    m /= 10;
    }
    
                cout << "Summa:" << sum <<endl;
                sum=0;
                }
    
        return 0;
    }
    


  • hi @Tenger
    this is first time i write python code, i struggled a little bit because of the specific syntax, but the rest is very similar to c++

    disclaimer : i have 15 minute of Pyhton background, there may be an error in the code

    print("Determine the sum of the first n digits of m\n")
    n = 0
    m = 0
    sum=0
    print("Enter m and n integers\n")
    m = int(input("Type the Number (m) : "))
    n = int(input("Type digit count (n) : "))
    copy = m
    x = 1
    while( copy > 0): 
      x += 1
      copy = copy / 10
    c = x - n
    if c < 0 : 
     print("Not enough digits\n")
    else : 
     for j in range(c) : 
      m /= 10
      while(m>0) : 
       sum += int(m%10)
       m/=10
     print("Sum is %s" %int(sum))        
    

    note: your question is not really qt related

    https://forum.qt.io/topic/107733/c-c-programming-homework-help



  • Hi @LeLev
    It's so close working :D But anyway thank you, i'll try to rebuild something. And yes, sorry, i'm first time writting there and don't know where and what i must to write. Thank you and sorry! :D


  • Banned

    Let me get this straight you have a class assignment to take C++ code and convert the syntax to Python but instead of doing it your self you come to the internet to get someone to do it for you. You do realize that is the same thing as having someone else write your paper for a spoken/written language course and that is considered to be horribly wrong by all educational institutes. As well as it should be because you do not learn anything that way.

    Now however if you need help understanding a specific bit of syntax that is different then I can explain just about anything you will need without doing the actual work for you. Further with all the "examples" of how to do Python out there and with Python being very similar to C++ structurally this ought to be a fairly easy conversion. I mean its not like having to convert Assembly into say Cobol or vice-versa that would be a chore and a true challenge.

    Next to start a true pythonic program you need to implement the

    if __name__ == '__main__':
    

    functionality. Sure you can write a python program without it but that is not the best methodology for writing something that is truly pythonic so with that denoted and using the examples out on the internet what do you think your next few lines ought to be?


Log in to reply