Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python additional sample #1

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion C#/STPWithClass/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static void Main(string[] args)
case "show":
for (int i = 0; i < items.Count; i++)
{
Console.WriteLine($"{i}:{items[i].GetIntroTxt()}");
Console.WriteLine($"{i}:{items[i].GetIntroTxt()}");
}
break;
case "buy":
Expand Down
50 changes: 50 additions & 0 deletions Python/STP_with_class/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Item:
def __init__(self,name,value):
self.name=name
self.value=value
self.count=0

def Buy(self):
self.count+=1

def GetAmountOfMoney(self):
return self.value*self.count

def GetIntroTxt(self):
return f"{self.name}:{self.value}円"

def GetSum():
total=0
for item in items:
total+=item.GetAmountOfMoney()
return total

def AddItem(cmds):
name=cmds[1]
value=int(cmds[2])
newItem=Item(name,value)
items.append(newItem)
print(f"{value}円の{name}を登録しました")

def CheckOut():
for item in items:
print(item.GetIntroTxt()+f"*{item.count}")
print(f"合計金額:{GetSum()}円")

items=[]
while True:
cmd=input()
cmds=cmd.split(" ")
if cmds[0]=="exit":
break
elif cmds[0]=="add":
AddItem(cmds)
elif cmds[0]=="show":
for i,item in enumerate(items):
print(f"{i}:{item.GetIntroTxt()}")
elif cmds[0]=="buy":
targetindex=int(cmds[1])
print(f"{items[targetindex].name}を一個購入しました")
items[targetindex].Buy()
elif "checkout":
CheckOut()
14 changes: 7 additions & 7 deletions Python/STP_without_class/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
item_names.append(name)
item_values.append(value)
item_counts.append(0)
print('{}円の{}を登録しました'.format(value, name))
print(f'{value}円の{name}を登録しました')
if cmds[0] == 'show':
for i in range(len(item_names)):
print('{}:{}:{}円'.format(i, item_names[i], item_values[i]))
print(f'{i}:{item_names[i]}:{item_values[i]}円')
if cmds[0] == 'buy':
target_index = int(cmds[1])
item_counts[target_index] += 1
print('{}を購入しました'.format(item_names[target_index]))
print(f'{item_names[target_index]}を購入しました')
if cmds[0] == 'checkout':
sum = 0
total = 0
for i in range(len(item_names)):
sum += item_values[i] * item_counts[i]
print('{} {}円×{}'.format(item_names[i], item_values[i], item_counts[i]))
total += item_values[i] * item_counts[i]
print(f'{item_names[i]} {item_values[i]}円×{item_counts[i]}')
item_counts[i] = 0
print('合計金額:{}円'.format(sum))
print(f'合計金額:{total}円')