2022年5月29日 星期日

PYTHON GS100

 

# 計數
import pygame
import sys

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)


def main():
pygame.init()
pygame.display.set_caption("first Pygame")
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
font = pygame.font.Font(None, 80)
tmr = 0

while True:
tmr = tmr + 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
txt = font.render(str(tmr), True, WHITE)
screen.fill(BLACK)
screen.blit(txt, [300, 200])
pygame.display.update()
clock.tick(10)


if __name__ == '__main__':
main()


PYTHON GS001

 

# 入門篇的 4 
import calendar
import datetime

# 4-2

# print(calendar.month(2022,6))
# print(calendar.isleap(2024))

# # 4-3

# print(datetime.date.today())
# print(datetime.date.fromisocalendar(2011, 5, 3))
# print(datetime.datetime.now())

# d = datetime.datetime.now()
# print(d.hour)
# print(d.minute)
# print(d.second)
# # ^ 所以這邊是產了一個名為 d datetime.datetime.now() 物件嗎?
#
# dd = datetime
# print(dd.datetime.now().hour)
# print(dd.datetime.now().minute)
# print(dd.datetime.now().second)
# # ^ 看來我猜得沒錯。

today = datetime.date.today()
birth = datetime.date(1982, 4, 16)
print(today - birth)

# 4-4


2022年5月27日 星期五

PYTHON GS000基本練習

 

# 入門篇基礎
import calendar

# --在 pycharm 上的快捷鍵--
# 範圍選取:[shift]+[上、下、左、右]
# 註解或反註解:[ctrl]+[/] (可搭配範圍選取)
# 整列程式碼上下移動:[ctrl]+[shift]+[上、下] (可搭配範圍選取)
# 執行程式:[shift]+[F10]

"""
多行註解
行1
行2
"""

#2-3
# print(calendar.month(2022, 5))
# ^印出五月的月曆
# print(calendar.prcal(2022))
# ^印出2022年的月曆

# 2-6
# print("輸入名字~~")
# name = input()
# print(f"name:{name}")
# print(input())


# 3-1

# a = ("AB" + "CD") * 2
# print(a*2)
# ^ 字串能相加也能乘整數,但不能相減,也不能除數字。

# 3-2
# card = []
# card.append(1)
# card.append(2)
# card.append("")
# print(card)
# # ^可後先設定空白列表,再使用 .append() 增加元素。
# enemy = ["史萊姆", "骷髏士兵", "魔法師"]
# print(enemy)
# enemy.append("哥布林")
# print(enemy)

# 3-3
# if False:
# print("Hei Jia-Jia")
# ^除了 True False 外,還有運算子 ==, !=, >, <, >=, <=

# life = 0
# if life > 0:
# print("活著~~")
# else:
# print("死了~~")

# 3-4

# for i in range(10):
# print(i)

# for hei in range(10,-1,-3):
# print(hei)
# ^ 10 -1 ,每次加 -3

# hei = 1
# hej = 10
# while hei < hej:
# print(f"{hei} < {hej}")
# hei +=1
# if hei ==5:
# print("要中斷了!")
# break

# 3-5

# def fun1():
# print("執行了函數fun1")


# fun()

# def fun2():
# fun1()
# print("執行了函數fun2")
# ^函數可以呼叫其他函數。

# fun2()

# def fun3(hei):
# print("執行了函數fun3")
# if hei <10:
# hei +=2
# fun3(hei)
# ^函數也可以自己呼叫自己,但要設好條件,不然會跑不完。

# hei = 1
# fun3(hei)

# def fun4(hei,jia):
# return hei + jia
# ^ 有回傳值的函數。


# print(fun4(1, 10))
# print(fun4("", "嘉嘉"))
# ^在這個例子中,送入的參數可以是文字,也可以是數字。

def fun5():
return "黑嘉嘉是我老婆!"
# ^沒有給參數,也可以有回傳值。

print(fun5())

2022年5月21日 星期六

列出某數字以下的所有質數

 這應該是我第三次做這個練習了,每次都用不同的語言,這次是用 C# ,寫起來比 python 方便,據說 Fortran 做數值運算比其他語言有效率,或許可以試看看它有多快。

using System;

namespace PrimeNumber

{

    class Program

    {

        static void Main(string[] args)

        {

            bool u;

            int sum = 0;

            for (int hei = 5; hei < 1000000000; hei += 2)

            {

                u = true;

                for (int hej = 3; hej <= (hei / 2); hej += 2)

                {

                    if (hei % hej == 0)

                    {

                        u = false;

                        break;

                    }

                }

                if (u)

                {

                    Console.Write(hei + " ");

                    sum++;

                }

            }

        }

    }

}