Decided to work on porting at least part of the Python program I had done with the video on Youtube into C#, and am really happy with getting the original core part working.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TryingStuff1
{
class Program
{
static void Main(string[] args)
{
string user_input= "";
while (user_input != "quit")
{
int calculation_to_units = 24;
string name_of_unit = "hours";
Console.Write("Enter the number of days. Enter 'quit' to exit the program: ");
user_input = Console.ReadLine();
Console.Clear();
if (user_input.ToLower() == "quit")
{
System.Environment.Exit(0);
}
else if (int.TryParse(user_input, out int number))
{
int user_input_int;
if (int.Parse(user_input) <= 0)
{
Console.WriteLine("Please enter a positive integer.");
}
else
{
user_input_int = int.Parse(user_input);
int total_time = user_input_int * calculation_to_units;
string conversion_output = String.Format("{0} days are {1} {2}", user_input_int, total_time, name_of_unit);
Console.WriteLine(conversion_output);
continue;
}
}
else
Console.WriteLine("Please enter an integer or 'quit'.");
}
}
}
}
Code language: C# (cs)
The C# code does not have the functions like the Python code, or the sets being used, but is still doing almost all of the same work.
#! /bin/python
calculation_to_units = 24
name_of_unit = "hours"
def days_to_units(number_of_days):
if number_of_days > 0:
return f"{number_of_days} days are {number_of_days * calculation_to_units} {name_of_unit}"
else:
return "You did not enter a positive integer..."
def validate_and_execute(validated_days):
try:
print(days_to_units(int(validated_days)))
except ValueError:
print("Please enter a positive integer value...")
user_input = 0
while user_input != "quit":
user_input = input("""Enter the number of days (comma separated lists are accepted (e.g. 10, 20, 30).\nEnter 'quit' to exit the program: """)
if user_input.lower() == "quit":
quit()
list_of_days = user_input.split(", ")
for num_of_days in set(list_of_days):
validate_and_execute(num_of_days)
Code language: Python (python)