try:
initial_balance = int(input(“Initial Balance? “))
except ValueError:
print(“Invalid input, exiting … “)
exit()
RATE = 1.1
for year in range(3):
print(f”Year {year}: {initial_balance * RATE ** year:,.2f}”)
カテゴリー: Python
Python 真偽値
while ”:
command = int(input(“Select 1, 2, 3 (0: Exit) “))
match command:
case 1:
print(“Menu 1”)
case 2:
print(“Menu 2”)
case 3:
print(“Menu 3”)
case 0:
break
case _:
print(“Invalid command, try again”)
continue
print(“Menu processed correctly”)
python break,continue
while 1 == 1:
command = int(input(“Select 1, 2, 3 (0: Exit) “))
match command:
case 1:
print(“Menu 1”)
case 2:
print(“Menu 2”)
case 3:
print(“Menu 3”)
case 0:
break
case _:
print(“Invalid command, try agein”)
continue
print(“Menu processed correctly”)
Python while
while 1 == 1:
command = int(input(“Select 1, 2, 3 (0: Exit) “))
match command:
case 1:
print(“Menu 1”)
case 2:
print(“Menu 2”)
case 3:
print(“Menu 3”)
case 0:
pass
Python while
command = int(input(“Select 1,2,3 (0: Exit)”))
while command != 0:
match command:
case 1:
print(“Menu 1”)
case 2:
print(“Menu 2”)
case 3:
print(“Menu 3”)
command = int(input(“Select 1,2,3 (0: Exit)”))
python forの中でforを使ってみる
initial_balance = int(input(“Initial Balance? “))
RATE = 1.1
for i in range(3):
if i != 0:
initial_balance += 10_000
for year in range(3):
print(f”Year {year}: {initial_balance * RATE ** year:,.2f}”)
Python forとf文字を組み合わせる
initial_balance = int(input(“Initial Balance? “))
RATE = 1.1
print(f”Year 0: {initial_balance:,.2f}”)
print(f”Year 1: {initial_balance * RATE:,.2f}”)
print(f”Year 2: {initial_balance * RATE * RATE:,.2f}”)
print(f”Year 0: {initial_balance * RATE ** 0:,.2f}”)
print(f”Year 1: {initial_balance * RATE ** 1:,.2f}”)
print(f”Year 2: {initial_balance * RATE ** 2:,.2f}”)
for i in range(3):
print(f”Year {i}: {initial_balance * RATE ** i:,.2f}”)
for year in range(10):
print(f”Year {year}: {initial_balance * RATE ** year:,.2f}”)
python for
#for i in range(10):
for i in range(8, 5, -1):
print(f”{i} Hello!”)
python matchとif
initial_balance = int(input(“Initial Balance? “))
match initial_balance:
case n if n >= 100_000:
RATE = 1.1
case n if n >= 80_000:
RATE = 1.08
case n if n >= 60_000:
RATE = 1.06
case _:
RATE = 1.01
print(f”Current rate: {RATE:.2f}”)
print(f”Year 0: {initial_balance:,.2f}”)
print(f”Year 1: {initial_balance * RATE:,.2f}”)
print(f”Year 2: {initial_balance * RATE * RATE:,.2f}”)
Python ifelse
initial_balance = int(input(“Initial Balance? “))
RATE = 1.1 if initial_balance >= 100_000 else 1.01
print(f”Current rate: {RATE:.2f}”)
print(f”Year 0: {initial_balance:,.2f}”)
print(f”Year 1: {initial_balance * RATE:,.2f}”)
print(f”Year 2: {initial_balance * RATE * RATE:,.2f}”)