neils011
Posts: 1
Joined: Thu Jun 10, 2021 2:57 pm

Need help converting c# code to python

Thu Jun 10, 2021 3:00 pm

Im trying to convert this piece of c# code to python but I just cant figure it out. ANy help would be great

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class EmployeeSystem
{
List<Employee> lstEmployee = new List<Employee>();
public int countOfEmployee { get; set; }
public static bool AddMore=true;
static void Main(string[] args)
{
Console.WriteLine("Employee Management System");
{
EmployeeSystem obj=new EmployeeSystem();
obj.RunProgram();
}
}
public void RunProgram()
{ int ID=0;  
while(AddMore)
{
ID++;
Console.WriteLine("Do you want to add Employee?y/n");
string ans = Console.ReadLine().ToLower();
if (ans == "y")
{
AddEmployee(ID);
}
else
{
PrintEmployeeLIst();
AddMore=false;
}
}
}
public void AddEmployee(int Id)
{

Console.WriteLine("Enter Employee Name");
string name = Console.ReadLine();
Console.WriteLine("Is this Employee a Manager?y/n");
string IsManger = Console.ReadLine().ToLower();
if (IsManger == "y")
{
Manager objManager = new Manager();
objManager.ID = Id;
objManager.Name = name;
Console.WriteLine("No of subordianates");
int noofSubordinate = Convert.ToInt32(Console.ReadLine());
Employee[] arrSubordinates = new Employee[noofSubordinate];
for (int i = 0; i < noofSubordinate; i++)
{
Employee objEmployee = new Employee();
objEmployee.ID = Id;
objEmployee.Name = name;
Console.WriteLine("subordianates Name?");
string subordianteName = Console.ReadLine();

arrSubordinates[i] = objEmployee;

}
objManager.subordinates = arrSubordinates;
lstEmployee.Add(objManager);
}
else
{
Employee objEmployee = new Employee();
objEmployee.ID = Id;
objEmployee.Name = name;
lstEmployee.Add(objEmployee);
}

}
public void PrintEmployeeLIst()
{
foreach (Employee emp in lstEmployee)
{
Console.WriteLine(emp.Name);
Manager m = (Manager)emp;
if (m.noOfSubordinates > 0)
{
foreach (Employee subordinate in m.subordinates)
{
Console.WriteLine(subordinate.Name);
}
}
  
}
}
}
//Employee Class
class Employee
{
public int ID { get; set; }
public string Name { get; set; }

public Employee()
{
}
public Employee(string Name)
{

}
}
//Manager class
class Manager : Employee
{
public Employee[] subordinates { get; set; }
public int noOfSubordinates { get; set; }
public Manager()
{
}
public Manager(string Name)
{
}
public void PrintSubordinateList(Employee[] subordinates)
{
foreach (Employee subordinate in subordinates)
Console.WriteLine(subordinate.Name);
}
public void AddSubordinate(Employee employee)
{
}
}

}

User avatar
B.Goode
Posts: 12304
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Need help converting c# code to python

Thu Jun 10, 2021 4:16 pm

neils011 wrote:
Thu Jun 10, 2021 3:00 pm
Im trying to convert this piece of c# code to python but I just cant figure it out. ANy help would be great

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class EmployeeSystem
{
List<Employee> lstEmployee = new List<Employee>();
public int countOfEmployee { get; set; }
public static bool AddMore=true;
static void Main(string[] args)
{
Console.WriteLine("Employee Management System");
{
EmployeeSystem obj=new EmployeeSystem();
obj.RunProgram();
}
}
public void RunProgram()
{ int ID=0;  
while(AddMore)
{
ID++;
Console.WriteLine("Do you want to add Employee?y/n");
string ans = Console.ReadLine().ToLower();
if (ans == "y")
{
AddEmployee(ID);
}
else
{
PrintEmployeeLIst();
AddMore=false;
}
}
}
public void AddEmployee(int Id)
{

Console.WriteLine("Enter Employee Name");
string name = Console.ReadLine();
Console.WriteLine("Is this Employee a Manager?y/n");
string IsManger = Console.ReadLine().ToLower();
if (IsManger == "y")
{
Manager objManager = new Manager();
objManager.ID = Id;
objManager.Name = name;
Console.WriteLine("No of subordianates");
int noofSubordinate = Convert.ToInt32(Console.ReadLine());
Employee[] arrSubordinates = new Employee[noofSubordinate];
for (int i = 0; i < noofSubordinate; i++)
{
Employee objEmployee = new Employee();
objEmployee.ID = Id;
objEmployee.Name = name;
Console.WriteLine("subordianates Name?");
string subordianteName = Console.ReadLine();

arrSubordinates[i] = objEmployee;

}
objManager.subordinates = arrSubordinates;
lstEmployee.Add(objManager);
}
else
{
Employee objEmployee = new Employee();
objEmployee.ID = Id;
objEmployee.Name = name;
lstEmployee.Add(objEmployee);
}

}
public void PrintEmployeeLIst()
{
foreach (Employee emp in lstEmployee)
{
Console.WriteLine(emp.Name);
Manager m = (Manager)emp;
if (m.noOfSubordinates > 0)
{
foreach (Employee subordinate in m.subordinates)
{
Console.WriteLine(subordinate.Name);
}
}
  
}
}
}
//Employee Class
class Employee
{
public int ID { get; set; }
public string Name { get; set; }

public Employee()
{
}
public Employee(string Name)
{

}
}
//Manager class
class Manager : Employee
{
public Employee[] subordinates { get; set; }
public int noOfSubordinates { get; set; }
public Manager()
{
}
public Manager(string Name)
{
}
public void PrintSubordinateList(Employee[] subordinates)
{
foreach (Employee subordinate in subordinates)
Console.WriteLine(subordinate.Name);
}
public void AddSubordinate(Employee employee)
{
}
}

}


All sorts of possibly-annoying non-answers come to mind...

What model of Raspberry Pi board will the resulting code be executed on, and what Operating System will it be running?

What is your familiarity with the two languages involved?

How far have you got: where have you got stuck?



As someone who knows nothing of C#, and only a little more about Python, I think I would start by regarding the C# code as a form of pseudo-code that provides an outline program specification, and base a Python solution on that.

I would start by using Python to write outline definitions of the corresponding Classes, Objects and Methods, and gradually fill in the executable code.


Edit: added later - using an Internet Search Tool with the target "convert c# to python" offers some automated tools. But they complain that your original c# code is not valid so produce no result.

Heater
Posts: 18369
Joined: Tue Jul 17, 2012 3:02 pm

Re: Need help converting c# code to python

Wed Jun 16, 2021 11:54 pm

This looks like a homework assignment.

Generally most folks don't like to do other peoples homework for them.

In this case it looks like a simple object oriented example of using inheritance. The problem then might be how to do inheritance in Python: https://www.w3schools.com/python/python_inheritance.asp
Memory in C++ is a leaky abstraction .

Return to “General programming discussion”