標題:
我在家裡練習的一些代碼
[打印本頁]
作者:
郭詩瑜
時間:
2022-4-19 17:46
標題:
我在家裡練習的一些代碼
一些我在家裡練習的Code
作者:
郭詩瑜
時間:
2022-4-19 17:49
首字縮寫
def f1(h):
for i in range(h):
x = input("Input your phrase or sentence: ")
phrase = x.split()
acronym = ""
for word in phrase:
acronym = acronym + word[0].upper()
print(f"Acronym of {x} is {acronym}.")
return " "
print(f1(h=int(input("How many rounds you want to run this program?: "))))
複製代碼
作者:
郭詩瑜
時間:
2022-4-19 17:51
本帖最後由 郭詩瑜 於 2022-4-20 16:00 編輯
個人信息整理
y = []
def f1():
x = input("請輸入你的姓名: ")
if (str.isalnum(x) == True):
y.append(x)
return ""
else:
print("請勿輸入特殊字元")
print(f1())
def f2():
x = int(input("請輸入你的生年(數字): "))
if (2100 > x > 1800):
y.append(x)
else:
print("請勿輸入不實數據")
print(f2())
exit()
x = int(input("請輸入你的生月(數字): "))
if (12 >= x >= 1):
y.append(x)
else:
print("請勿輸入不實數據")
print(f2())
exit()
x = int(input("請輸入你的生日(數字): "))
if (31 >= x >= 1):
y.append(x)
return " "
else:
print("請勿輸入不實數據")
print(f2())
exit()
def f3():
x = input("請輸入你的地址: ")
if (str.isalnum(x) == True):
y.append(x)
return " "
else:
print("請勿輸入特殊字元")
print(f3())
def f4():
x = input("請輸入你的目標: ")
if (str.isalnum(x) == True):
y.append(x)
return " "
else:
print("請勿輸入特殊字元")
print(f4())
print(f1(), f2(), f3(), f4())
print()
print(f"姓名: {y[0]}", end="\t")
print()
print(f"生日: {y[1]}年{y[2]}月{y[3]}日", end="\t")
print()
print(f"地址: {y[4]}", end="\t")
print()
print(f"目標: {y[5]}", end="\t")
print()
複製代碼
作者:
郭詩瑜
時間:
2022-4-19 17:52
本帖最後由 郭詩瑜 於 2022-4-20 18:51 編輯
Rock Paper Scissors
def rps(times):
y = ["Rock", "Paper", "Scissors"]
c = 0
h = 0
g = 0
while (g < times):
print(f"Round {g+1} : ")
x = input("Rock, Paper, Scissors: ")
x = x.replace(" ","")
from random import shuffle
shuffle(y),shuffle(y),shuffle(y),shuffle(y),shuffle(y)
z = y[0]
if (z == "Rock" and x == "Paper"):
print(f"Player = {x} Computer = {z}")
print("Player")
print()
h += 1
g += 1
elif (z == "Rock" and x == "Scissors"):
print(f"Player = {x} Computer = {z}")
print("Computer")
print()
c += 1
g += 1
elif (z == "Rock" and x == "Rock"):
print(f"Player = {x} Computer = {z}")
print("TIE")
print()
times += 1
g += 1
elif (z == "Paper" and x == "Rock"):
print(f"Player = {x} Computer = {z}")
print("Computer")
print()
c += 1
g += 1
elif (z == "Paper" and x == "Scissors"):
print(f"Player = {x} Computer = {z}")
print("Player")
print()
h += 1
g += 1
elif (z == "Paper" and x == "Paper"):
print(f"Player = {x} Computer = {z}")
print("TIE")
print()
times += 1
g += 1
elif (z == "Scissors" and x == "Paper"):
print(f"Player = {x} Computer = {z}")
print("Computer")
print()
c += 1
g += 1
elif (z == "Scissors" and x == "Rock"):
print(f"Player = {x} Computer = {z}")
print("Player")
print()
h += 1
g += 1
elif (z == "Scissors" and x == "Scissors"):
print(f"Player = {x} Computer = {z}")
print("TIE")
print()
times += 1
g += 1
elif (x != "Rock" or "Paper" or "Scissors" or "-1"):
print("Do Not Enter Unacceptable items")
print()
continue
elif (x == "-1"):
return "END BREAK OUT LOOP"
int(h)
int(c)
if (c > h):
print(f"Computer Score {c} Player Score {h}")
print("Computer Win")
return " "
elif (c < h):
print(f"Computer Score {c} Player Score {h}")
print("Player Win")
return " "
elif (c == h):
print(f"Computer Score {c} Player Score {h}")
print("Tie")
return " "
print(rps(times=int(input("How many time you want to play: "))))
while (True):
play = int(input("Do you want to play again? 1.Yes 2.No "))
if ( play == 1):
print(rps(times=int(input("How many time you want to play: "))))
print()
elif ( play == 2):
print("Thank You for Playing")
break
複製代碼
作者:
郭詩瑜
時間:
2022-4-20 10:20
本帖最後由 郭詩瑜 於 2022-4-21 14:04 編輯
猜樂透
def gng(how, many):
score = 0
times = 0
number = 0
guess_list = []
print("Guess number Game 1 - 49")
while (times < how):
while (number < many):
print(f"Number {number+1}")
y = int(input("Guess Number 1 - 49: "))
number += 1
guess_list.append(y)
if (0 >= y <= 49):
print("Please Do Not Enter Unacceptable number")
number -= 1
guess_list.pop()
z = list(range(1,49))
from random import shuffle
shuffle(z), shuffle(z), shuffle(z), shuffle(z), shuffle(z)
f = z[0:many]
if (len(set(f) & set(guess_list)) > 0):
final = set(f) & set(guess_list)
final = list(final)
final.sort()
f.sort()
print(f"[{len(final)}] Guess Is Correct, Correct Number is {final} Correct list is {f}")
score += len(final)
times+=1
elif (len(set(f) & set(guess_list)) <= 0):
print(f"All Wrong, The Correct Answer is {f}")
times+=1
guess_list.sort()
print(f"Your Guess is {guess_list}")
print(f"You Guess Total {score} out of {times * number} Number Correctly")
return " "
print(
gng(
how=int(input("How Many Times You Want To Play? : ")
),
many=int(input("How Many Number You want to Guess? :")
)
)
)
play = int(input("Do you want to play again? 1 = Yes, 2 = No:"))
if (play == 1):
print()
print(
gng(
how=int(input("How Many Times You Want To Play? : ")
),
many=int(input("How Many Number You want to Guess? :")
)
)
)
elif (play == 2):
print("Thank you for playing")
else:
print("Please Do Not Enter Unacceptable number")
複製代碼
作者:
郭詩瑜
時間:
2022-4-20 11:38
計算小費
print("計算小費")
print()
def count():
money = int(input("帳單金額: "))
tip = int(input("請輸入需要百分之幾的小費最小輸入1最高輸入99: "))
cal = money * (tip*0.01)
print(f"在費率{tip}%的情況下,小費為: {cal}, 總價為: {money + cal}")
print()
people = int(input("是否需要進行均攤 1.需要 2.不需要: "))
if (people == 1):
from decimal import Decimal
head = int(input("多少人進行均攤?: "))
value = Decimal(money/head).quantize(Decimal("0.00"))
cal_value = Decimal(cal/head).quantize(Decimal("0.00"))
all_value = Decimal((money + cal)/head).quantize(Decimal("0.00"))
print(f"消費均攤為:{value}, 小費均攤為:{cal_value}, 總價均攤為{all_value}")
print()
other = int(input("請問是否需要區別分價 1.需要 2.不需要: "))
if (other == 1):
big = int(input("大頭所需支付百分比最小輸入1最高輸入99: "))
big_value = big * 0.01
print()
print(f"含小費大頭須支付:{(money + cal) * big_value}, 不含小費為:{money * big_value}")
print(f"含小費小頭須支付:{((money + cal) - ((money + cal) * big_value))}, 不含小費為:{((money) - (money * big_value))}")
return "Thank You For Using"
elif (other == 2):
return "Thank You For Using"
else:
print("請勿輸入不合理值")
return "Thank You for Using"
elif ( people == 2):
print("好的")
print()
other = int(input("請問是否需要區別分價 1.需要 2.不需要: "))
if (other == 1):
big = int(input("大頭所需支付百分比最小輸入1最高輸入99: "))
big_value = big*0.01
print()
print(f"含小費大頭須支付:{(money+cal)*big_value}, 不含小費為:{money*big_value}")
print(f"含小費小頭須支付:{((money+cal)-((money+cal)*big_value))}, 不含小費為:{((money)-(money*big_value))}")
return "Thank You For Using"
elif (other == 2):
return "Thank You For Using"
else:
print("請勿輸入不合理值")
return "Thank You for Using"
else:
return "輸入無理值 Thank You For Using "
print(count())
複製代碼
作者:
郭詩瑜
時間:
2022-4-21 08:37
判斷籃球比分是否安全
def base():
print("Who will win the Basketball game")
score1 = int(input("Please input team 1 (Higher) score: "))
score2 = int(input("Please input team 2 (Lower) score: "))
score = float(score1 - score2)
score -= 3
ball = int(input("Who Got the ball 1.Team1 2. Team2: "))
if (ball == 1):
score += 0.5
elif (ball == 2):
score -= 0.5
elif (score < 0):
score -= 0
score = score ** 2
time = float(input("How many second left: "))
if (score > time):
print("The leading team is Safe")
print(pan())
return " "
else:
print("Not Safe")
print(pan())
return " "
def pan():
a = int(input("Do you want to use again? 1.yes 2.No: "))
if (a == 1):
print(base())
return " "
elif (a == 2):
print("Thank You for Using")
return " "
else:
print("Invalid input")
print("Thank You for Using")
return " "
print(base())
複製代碼
作者:
郭詩瑜
時間:
2022-4-21 12:12
本帖最後由 郭詩瑜 於 2022-4-21 14:00 編輯
猜數字
def game():
count = 0
number = list(range(1, 100))
from random import shuffle
shuffle(number), shuffle(number), shuffle(number)
final = int(number[0])
while (count <= 6):
count += 1
print(f"Round {count} ")
guess = int(input("Guess a Number from 1 -100: "))
if (guess > final):
print("High")
print()
elif (guess < final):
print("Low")
print()
if (guess == final):
print()
print(f"Round {count}")
print("You Won")
print()
print(again())
return " "
if (count == 7 or guess == -1):
print("You Lose")
print()
print(again())
return " "
def again():
x = int(input("Do you want to play again? 1.Yes 2. No: "))
if (x == 1):
print(game())
return " "
elif (x == 2):
print("Thank You for Playing")
return " "
else:
print("Invalid Input ")
print("Thank You for Playing")
return " "
print(game())
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2