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
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.
In theory, but the Python code is pretty self-explanatory.
Does it do what you want it to do?