Convert To C#

How do I categorise a multi language topic? In this case it’s Python and C#.
Link to project:
https://www.codecademy.com/courses/learn-object-oriented-programming-with-python/projects/create-a-game-using-classes-and-objects

I have this code:

class Food: def __init__(self, name = "sandwhich", price = 5, weight = "200g", stock = 1000, sold = False): self.name = 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"{self.name} of price ${self.price} has been sold out.") return print(f"{self.name} of price ${self.price} is sold.") return self.price def set_name(self, name): print(f"The item had a name of name of {self.name} but it's going to later have a name of {name}.") self.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 ((self.name[0] == "a") or (self.name[0] == "e") or (self.name[0] == "i") or (self.name[0] == "o")): description = f"An {self.name} with a price of ${self.price}. It has a stock of {self.stock} which means it is " else: description = f"A {self.name} 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): self.name = name self.money = money def greet(self): print("Hello {self.name} welcome to the shops.") def buy_food(self, food): if ((self.money - food.price ) < 0): print("Sorry, you don't have enough money. You have ${self.money} left over but the price of the {food.name} was {food.price}.") self.money -= food.buy() print("You have brought the the {food.name}.") def set_money(self, money): print("Welcome to the bank.") print(f"${money} coming out...") self.money += money def __repr__(self): return f"A customer named {self.name} who has {self.money}" 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 == product.name): 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 ${user.money} 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 user.money 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:
https://www.google.com/search?q=what+is+equivalent+to+try+except+else+finally+in+c%23&rlz=1C1CHZN_enAU959AU959&oq=what+is+equivalent+to+try+except+else+finally+in+c%23&aqs=chrome..69i57.20894j0j7&sourceid=chrome&ie=UTF-8
than this:
https://www.google.com/search?q=exception+handling+c%23&rlz=1C1CHZN_enAU959AU959&ei=vc01YaPZJuzF4-EP7amr8Ac&oq=exception+handling+c%23&gs_lcp=Cgdnd3Mtd2l6EAMyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgYIABAWEB4yBggAEBYQHjIGCAAQFhAeOgcIABBHELADOgQIABBDOgUIABCRAjoRCC4QgAQQsQMQgwEQxwEQ0QM6DgguEIAEELEDEMcBEKMCOgsILhCABBCxAxCDAToICAAQsQMQgwE6BwgAELEDEEM6CAgAEIAEELEDOgQIABAKSgUIOhIBMUoFCDwSATFKBAhBGABQnLkCWKmIA2DjigNoA3ACeACAAcMCiAHQHpIBCDAuMjEuMC4xmAEAoAEBsAEAyAEIwAEB&sclient=gws-wiz&ved=0ahUKEwjjq-vU8-nyAhXs4jgGHe3UCn4Q4dUDCA4&uact=5

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?
.strip():
This:
https://www.google.com/search?q=.strip()+method+C%23&rlz=1C1CHZN_enAU959AU959&oq=.strip()+method+C%23&aqs=chrome..69i57.7926j1j7&sourceid=chrome&ie=UTF-8

.title():
https://www.google.com/search?q=.title()+C%23&rlz=1C1CHZN_enAU959AU959&ei=Pvo1YZnyEbmd4-EPl9Gk0AQ&oq=.title()+C%23&gs_lcp=Cgdnd3Mtd2l6EAMyBggAEBYQHjIGCAAQFhAeMgYIABAWEB4yBggAEBYQHjIGCAAQFhAeMgYIABAWEB4yBggAEBYQHjIGCAAQFhAeMgYIABAWEB4yBggAEBYQHjoICAAQgAQQsQM6CAgAELEDEIMBOggILhCABBCxAzoFCAAQgAQ6BAgAEB46BwgAEMkDEB46CQgAEMkDEBYQHjoFCAAQkgM6CAgAEBYQChAeSgQIQRgAUMUMWLQsYOowaAFwAngAgAGvAYgBnw6SAQQwLjExmAEAoAEBsAEAwAEB&sclient=gws-wiz&ved=0ahUKEwiZiaSNnuryAhW5zjgGHZcoCUoQ4dUDCA4&uact=5

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:
https://www.google.com/search?q=exception+handling+c%23&rlz=1C1CHZN_enAU959AU959&oq=except&aqs=chrome.0.69i59j69i57j0i433i512l3j69i60l2j69i61.2720j0j7&sourceid=chrome&ie=UTF-8

Gone to here:

.

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

Does it do what you want it to do?