Convert To C#

How do I categorise a multi language topic? In this case it’s Python and C#.
Link to project:

I have this code:

class Food: def __init__(self, name = "sandwhich", price = 5, weight = "200g", stock = 1000, sold = False): = name self.price = price self.weight = weight self.stock = stock self.sold = sold if (self.stock < 0): self.stock = 0 def buy(self): self.stock -= 1 if (self.stock == 0): self.sold = True print(f"{} of price ${self.price} has been sold out.") return print(f"{} of price ${self.price} is sold.") return self.price def set_name(self, name): print(f"The item had a name of name of {} but it's going to later have a name of {name}.") = name print("Name is set.") def set_price(self, price): print(f"The item was ${self.price} but it is going to be ${price}.") self.price = price print(f"The price is now {self.price}.") def set_weight(self, weight): print(f"The item was {self.weight} but it's going to be {weight}.") self.weight = weight print(f"The weight is now {self.weight}.") def set_stock(self, stock): if (stock < 0): stock = 0 if (stock == 1): print(f"The item had {self.stock} stock before but later it is going to have {stock}.") else: print(f"The item had {self.stock} stocks before but later it is going to have {stock}.") self.stock = stock print("The stock is now {self.stock}.") def __repr__(self): if (([0] == "a") or ([0] == "e") or ([0] == "i") or ([0] == "o")): description = f"An {} with a price of ${self.price}. It has a stock of {self.stock} which means it is " else: description = f"A {} with a price of ${self.price}. It's weight is {self.weight}. It has a stock of {self.stock} which means it is " if (self.sold): description += "sold out." else: description += "not sold out." return description class User: def __init__(self, name = "Mario", money = 100): = name = money def greet(self): print("Hello {} welcome to the shops.") def buy_food(self, food): if (( - food.price ) < 0): print("Sorry, you don't have enough money. You have ${} left over but the price of the {} was {food.price}.") -= print("You have brought the the {}.") def set_money(self, money): print("Welcome to the bank.") print(f"${money} coming out...") += money def __repr__(self): return f"A customer named {} who has {}" sandwhich = Food() orange_juice = Food("250ml orange juice", weight = 500, stock = 100) burger = Food("burger", weight = 250) products = [sandwhich, orange_juice, burger] def shops(): while True: play = input("Would you like to buy some food? ").strip() if ((play == "yes") or (play == "no")): break else: print("You have to enter yes or no.") if (play == "no"): return "UNKOWN" name = input("Hello! What is your name? ").title() while True: try: money = int(input("How much money do you have? ").strip("$")) except Exception: print("You have to enter a number.") else: break user = User(name, money) user.greet() print("Here are the things you can buy:") for product in products: print(product) while True: while True: user_food = input("What do you want to buy? ").lower() for product in products: if (user_food == break print("That is not a food that we have available. Try again.") for product in products: if (user_food == product): user_food = product user.buy_food(product) print("You have ${} left.") while True: buy_again = input("Do you want to buy again? Enter yes or no. ") if ((buy_again == "yes") or (buy_again == "no")): break print("Please enter yes or no.") if (buy_again == "yes"): continue else: return break shops()

(there is a space between lines but I don’t want to fix it; it was just automatic)
I want to convert it to C# but I don’t know how to. I don’t know how to do these things:
What the equivalent of these things in C#:
__repr__ method
.strip() string method
.title() string method
.strip() string method
try, except, else and finally
Those are all the things I don’t know how to do in C# form(those are in Python form).

What do these do in Python? How about Googling the functionality in relation to C#?

Again, a simple Google search for error handling and logic flow in C# would suffice.

1 Like

I think the C# docs are the best for finding info.
But how do I categorise a multi language topic?
Is it better to search this:
than this:

I should search for things that would more likely appear on the page which is the second one(for the first one: people might not talk about Python and C#, try...finally is more longer). I know try..finally is called exception handling.
I have seen the const keyword when I was doing the Learn C# course. I found it in the docs. Should I just look at all the keywords?

No; that’d take far too long. Just Google the programming concepts. Logic. Error handling, etc.

This is really a C# problem, so I’d put it there.

But you need both Python and C# to answer all the questions.

Is this the right methods to search?


To this:
Is this the wrong one(it shows the system.Globalization namespace but I just want a string method):

Look that’s only 1 out of 2 possible ways(CultureInfo, TextInfo). Which way is better?
Look at TextInfo:

I’ll just do that way.

I’ve also found this:

Why is that way better than the string method .ToUpper()? Why that way?

Exception handling:
Up to here:

Gone to here:


In theory, but the Python code is pretty self-explanatory.

Does it do what you want it to do?