Click here to Skip to main content
15,885,880 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Help me convert this to python Thankyou


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    char Operation();
    string variableStr;
    double V1;
    double V2;
    double T1;
    double T2;

    cout << "V1/T1 = V2/T2" << endl << "Which variable are you solving for?" << endl;
    cin >> variableStr;

    system("clear");

    if (variableStr == "V1") {
        cout << "What is the value of T1 in Kelvin?" << endl;
        cin >> T1;

        cout << "What is the value of V2?" << endl;
        cin >> V2;

        cout << "What is the value of T2 in Kelvin?" << endl;
        cin >> T2;

        V1 = (V2/T2)*T1;
        return V1;

    } else if (variableStr == "V2") {
        cout << "What is the value of T1 in Kelvin?" << endl;
        cin >> T1;

        cout << "What is the value of T2 in Kelvin?" << endl;
        cin >> T2;

        cout << "What is the value of V1?" << endl;
        cin >> V1;

        V2 = (V1/T1)*T2;
        return V2;

    } else if (variableStr == "T1") {
        cout << "What is the value of T2 in Kelvin?" << endl;
        cin >> T2;
        cout << "What is the value of V1?" << endl;
        cin >> V1;
        cout << "What is the value of V2?" << endl;
        cin >> V2;
        T1 = V1/(V2/T2);
        return T1;
    } else if (variableStr == "T2") {
        cout << "What is the value of T1 in Kelvin?" << endl;
        cin >> T1;
        cout << "What is the value of V1?" << endl;
        cin >> V1;
        cout << "What is the value of V2?" << endl;
        cin >> V2;
        T2 = V2/(V1/T1);
        return T2;
    } else {
        cout << "Please enter a valid option!" << endl;
        char Operation();
    }
    return 0;
}


What I have tried:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    char Operation();
    string variableStr;
    double V1;
    double V2;
    double T1;
    double T2;

    cout << "V1/T1 = V2/T2" << endl << "Which variable are you solving for?" << endl;
    cin >> variableStr;

    system("clear");

    if (variableStr == "V1") {
        cout << "What is the value of T1 in Kelvin?" << endl;
        cin >> T1;

        cout << "What is the value of V2?" << endl;
        cin >> V2;

        cout << "What is the value of T2 in Kelvin?" << endl;
        cin >> T2;

        V1 = (V2/T2)*T1;
        return V1;

    } else if (variableStr == "V2") {
        cout << "What is the value of T1 in Kelvin?" << endl;
        cin >> T1;

        cout << "What is the value of T2 in Kelvin?" << endl;
        cin >> T2;

        cout << "What is the value of V1?" << endl;
        cin >> V1;

        V2 = (V1/T1)*T2;
        return V2;

    } else if (variableStr == "T1") {
        cout << "What is the value of T2 in Kelvin?" << endl;
        cin >> T2;
        cout << "What is the value of V1?" << endl;
        cin >> V1;
        cout << "What is the value of V2?" << endl;
        cin >> V2;
        T1 = V1/(V2/T2);
        return T1;
    } else if (variableStr == "T2") {
        cout << "What is the value of T1 in Kelvin?" << endl;
        cin >> T1;
        cout << "What is the value of V1?" << endl;
        cin >> V1;
        cout << "What is the value of V2?" << endl;
        cin >> V2;
        T2 = V2/(V1/T1);
        return T2;
    } else {
        cout << "Please enter a valid option!" << endl;
        char Operation();
    }
    return 0;
}
Posted
Updated 14-Apr-22 2:38am
Comments
Rick York 14-Apr-22 13:25pm    
NO!

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
 
Share this answer
 
Comments
CPallini 14-Apr-22 10:30am    
5.:-D
That is a very simple program. Read one (or more) of the many freely available Python tutorials, and 'do it yourself'.
 
Share this answer
 
Comments
OriginalGriff 14-Apr-22 9:10am    
But ... but ... it's my *homework*
Plzzz do it 4 me m8 ...

:sigh:
CPallini 14-Apr-22 10:31am    
Homework?!
Converting C++ to Python?
Are they really 'teaching' this stuff at school?
:-D
OriginalGriff 14-Apr-22 11:32am    
Nah. The task is very similar, so when they copy'n'pasted the whole question Google came back with a C++ version. They figure "who'll know?" and just try to get it converted so they can hand it in. Sad really.
CPallini 14-Apr-22 13:08pm    
That's reasonable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900