2017年9月21日 星期四

資料結構

判斷質數(1)
i=2
prime=0
num = int(input("輸入一個整數:"))
while (i<=num**0.5):
   
    if (num%i==0):
        prime=1
    i+=1
   
if (prime==1):
    print("%s不是質數"%num)
else:
    print("%s是質數"%num)

判斷質數(2)
num = int(input("請輸入一個數字: "))

# 質數大於 1
if num > 1:
   # 查看因子
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"不是質數")
           print(i,"乘與",num//i,"是",num)
           break
   else:
       print(num,"是質數")
       
# 如果輸入的數字小於或等於 1,不是質數
else:
   print(num,"不是質數")

九九乘法表
for i in range(1,10):
    for j in range(1,10):
        print(i,"*",j," = ","%2d"%(i*j),sep='',end='  ')
    print()

計算輸入的數的平均和總和

nums = count = 0

while True:
    num = int(input("輸入一個成績"))
    if num == -1:
        print('總成績為:',nums,'平均成績為:',nums/count)
        break
    
    nums += num
    count += 1


2017/10/12








2017/10/19

point = [
         [-1, 3],
         [-1, -1],
         [1, 1],
         [2, 0.5],
         [2, -1],
         [3, 3],
         [4, 2],
         [4, -0.5]
        ]

def distance(A, B):
    return((A[0]-B[0])**2 + ((A[1]-B[1])**2))**0.5

for a in point:
    for b in point:
        print(round(distance(a,b),1),'',end='')
print()




輸出一亂數
判斷是否為奇數,若是的話就把這數字「*3+1」,若否的話就「/2」,這是一個迴圈,直到數字n變為1才停止

import random
rand_num = random.randint(100,1000)
orig_rand_num = rand_num
loop_count = 0
every_step = []

while True:
    every_step.append(int(rand_num))
    if(rand_num == 1): break
    if(rand_num % 2 == 0):
        rand_num = rand_num/2
    else:
        rand_num = rand_num*3 + 1
    loop_count +=1

print("亂數 = ",orig_rand_num)
print("運算次數 = ",loop_count)
print("運算過程 : ",every_step)


建立一個字典
搜尋字典內的Key,然後輸出Value

name_dict = {}
key = ""
SearchKey = ""
print("Create dictionary:")

while key != "end":
    key = input("key:")
    if key == "end":
        break
    else:
        value = input("value:")
        name_dict[key] = value
        
while SearchKey != "end":
    SearchKey = input("Search key:")
    if SearchKey == "end":
        break
    else:
        if(SearchKey in name_dict) == False:
            print("Value:no such key")
        else:
            print("Value:"+name_dict[SearchKey])

open()使用方法
content = '''hello python
逢甲應用數學系
Welcome'''

f = open("file1.txt",'w')
f.write(content)
f.close()

f = open("file1.txt",'r')
for line in f:
    print(line)   
f.close()

with open("file1.txt",'r')as f:
    for line in f:
        print(line)

Tk1
import tkinter as tk
#主視窗名稱 = tk.Tk()
win = tk.Tk()
win.geometry("200x100")
win.title("這是主視窗")

lable = tk.Label(win,text = "hello world")
lable.pack()

cont = 0
def b():
    global cont
    cont += 1
    textvar.set("按我")
    lable.pack()


textvar = tk.StringVar()
button = tk.Button(win,textvariable = textvar,command = b)
textvar.set("按我")
button.pack()

win.mainloop()


Tk2
import tkinter as tk

win = tk.Tk()
win.title("這是主視窗")
frame1 = tk.Frame(win)
frame1.pack()
lable1 = tk.Label(frame1,text = "username:")
entry1 = tk.Entry(frame1)
lable1.grid(row = 0,column = 0)
entry1.grid(row = 0,column = 1)

frame2 = tk.Frame(win)
frame2.pack()
button1 = tk.Button(frame2,text = "yes")
button2 = tk.Button(frame2,text = "NO")

button1.grid(row = 0,column = 0)
button2.grid(row = 0,column = 1)

win.mainloop()

2016年12月30日 星期五

物件導向#C - 期末作業

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
    Text mytxt;
    Text mytxt1;
    Text mytxt2;
    Text ctxt;
    Text ctxt1;
    Text ctxt2;
    Text c1txt;
    Text c2txt;
    Text c3txt;
    Image img;
    Image img1;
    Image img2;

    int Mmoney = 0;
    int cmoney1 = 0;
    int cmoney2 = 0;
    int cmoney3 = 0;
    // Use this for initialization
    void Start()
    {
        Mmoney = 138;
        mytxt = GameObject.Find("Canvas/Panel/Button/Text").GetComponent<Text>();
        mytxt1 = GameObject.Find("Canvas/Panel/Button1/Text").GetComponent<Text>();
        ctxt = GameObject.Find("Canvas/Panel/Text").GetComponent<Text>();
        ctxt1 = GameObject.Find("Canvas/Panel/Text (1)").GetComponent<Text>();
        ctxt2 = GameObject.Find("Canvas/Panel/Text (2)").GetComponent<Text>();
        c1txt = GameObject.Find("Canvas/Panel/Button2/Text").GetComponent<Text>();
        c2txt = GameObject.Find("Canvas/Panel/Button3/Text").GetComponent<Text>();
        c3txt = GameObject.Find("Canvas/Panel/Button4/Text").GetComponent<Text>();
        img = GameObject.Find("Canvas/Panel/Image").GetComponent<Image>();
        img1 = GameObject.Find("Canvas/Panel/Image1").GetComponent<Image>();
        img2 = GameObject.Find("Canvas/Panel/Image2").GetComponent<Image>();
    }

    // Update is called once per frame
    void Update()
    {
        mytxt.text = Mmoney.ToString();
        c1txt.text = cmoney1.ToString();
        c2txt.text = cmoney2.ToString();
        c3txt.text = cmoney3.ToString();
    }
    public void myfun()
    {
        Mmoney = Mmoney + 1;

    }
    public void mygame()
    {
        int rnum;
        rnum = Random.Range(0, 3);
        mytxt1.text = rnum.ToString();
        if (rnum == 0)
        {
            img.color = UnityEngine.Color.red;
            img1.color = UnityEngine.Color.black;
            img2.color = UnityEngine.Color.black;
            ctxt.text = "1";
            ctxt1.text = "0";
            ctxt2.text = "0";
            cmoney1 = cmoney1 * 2;
            cmoney2 = 0;
            cmoney3 = 0;
            Mmoney = Mmoney + cmoney1;
            cmoney1 = 0;

        }
        if (rnum == 1)
        {
            img.color = UnityEngine.Color.black;
            img1.color = UnityEngine.Color.red;
            img2.color = UnityEngine.Color.black;
            ctxt.text = "0";
            ctxt1.text = "1";
            ctxt2.text = "0";
            cmoney2 = cmoney2 * 2;
            cmoney1 = 0;
            cmoney3 = 0;
            Mmoney = Mmoney + cmoney2;
            cmoney2 = 0;

        }
        if (rnum == 2)
        {
            img.color = UnityEngine.Color.black;
            img1.color = UnityEngine.Color.black;
            img2.color = UnityEngine.Color.red;
            ctxt.text = "0";
            ctxt1.text = "0";
            ctxt2.text = "1";
            cmoney2 = 0;
            cmoney1 = 0;
            cmoney3 = cmoney2 * 2;
            Mmoney = Mmoney + cmoney3;
            cmoney3 = 0;

        }


    }
    public void c1()
    {
        cmoney1 = cmoney1 + 1;

        Mmoney = Mmoney - 1;

    }
    public void c2()
    {
        cmoney2 = cmoney2 + 1;

        Mmoney = Mmoney - 1;
    }
    public void c3()
    {
        cmoney3 = cmoney3 + 1;

        Mmoney = Mmoney - 1;


    }
}




2016年12月22日 星期四

物件導向C# - 猜猜看可得分或失分

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
    Text mytxt;
    Text mytxt1;
    Text mytxt2;
    Text mytxt3;
    Text mytxt4;
    Text mytxt5;
    int mymoney1 = 100;
    int mymoney2 = 100;
    int mymoney3 = 100;
    // Use this for initialization
    void Start()
    {

        mytxt = GameObject.Find("Canvas/Panel/Text").GetComponent<Text>();
        mytxt1 = GameObject.Find("Canvas/Panel/Text1").GetComponent<Text>();
        mytxt2 = GameObject.Find("Canvas/Panel/Text2").GetComponent<Text>();
        mytxt3 = GameObject.Find("Canvas/Panel/Text3").GetComponent<Text>();
        mytxt4 = GameObject.Find("Canvas/Panel/Text4").GetComponent<Text>();
        mytxt5 = GameObject.Find("Canvas/Panel/Text5").GetComponent<Text>();
        mytxt2.text = mymoney1.ToString();
        mytxt3.text = mymoney2.ToString();
        mytxt5.text = mymoney3.ToString();

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void myfun()
    {
        int num = 0, num1 = 0, num2 = 0;


        num = Random.Range(0, 2);
        num1 = Random.Range(0, 2);

        if (num == 0)
        {
            num1 = 1;
            num2 = 0;
            mymoney1 = mymoney1 + 0;
            mymoney2 = mymoney2 + 2;
            mymoney3 = mymoney3 + 0;

        }
        else if (num1 == 0)
        {
            num = 0;
            num2 = 1;
            mymoney1 = mymoney1 + 0;
            mymoney2 = mymoney2 + 0;
            mymoney3 = mymoney3 + 2;

        }
        else
        {
            num1 = 0;
            num2 = 0;
            mymoney1 = mymoney1 + 2;
            mymoney2 = mymoney2 + 0;
            mymoney3 = mymoney3 + 0;
        }


        mytxt.text = num.ToString();
        mytxt1.text = num1.ToString();
        mytxt4.text = num2.ToString();

        mytxt2.text = mymoney1.ToString();
        mytxt3.text = mymoney2.ToString();
        mytxt5.text = mymoney3.ToString();


        //Image img = GameObject.Find ("Image").GetComponent<Image> ();
        //img.color = UnityEngine.Color.red;
        //mytxt1.text =Random.Range(1,2).ToString();
        //float translation = Time.deltaTime * 10;
    }


    public void myfun1()
    {
        mymoney1 = mymoney1 - 1;
        mytxt2.text = mymoney1.ToString();

    }
    public void myfun2()
    {
        mymoney2 = mymoney2 - 1;

        mytxt3.text = mymoney2.ToString();
    }
    public void myfun3()
    {
        mymoney3 = mymoney3 - 1;

        mytxt5.text = mymoney3.ToString();
    }

}




2016年12月15日 星期四

物件導向C# - 紅綠燈

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Counter : MonoBehaviour {
int num;
double fMyWaitTime=4.0;
double WaitTimecount=0;
// Use this for initialization
void Start () {
num = 0;
}

// Update is called once per frame
void Update () {

Text mytxt=GameObject.Find("Text").GetComponent<Text>();
num = num + 1;
int countnum = num % 4;
mytxt.text =countnum.ToString();
WaitTimecount=0;
mywait ();

if (countnum==0)
{
Image img = GameObject.Find ("Image1").GetComponent<Image> ();
img.color = new Color(1.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==1)
{
Image img = GameObject.Find ("Image1").GetComponent<Image> ();
img.color = new Color(0.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==2)
{
Image img = GameObject.Find ("Image1").GetComponent<Image> ();
img.color = new Color(0.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==3)
{
Image img = GameObject.Find ("Image1").GetComponent<Image> ();
img.color = new Color(0.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==0)
{
Image img = GameObject.Find ("Image").GetComponent<Image> ();
img.color = new Color(0.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==1)
{
Image img = GameObject.Find ("Image").GetComponent<Image> ();
img.color = new Color(0.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==2)
{
Image img = GameObject.Find ("Image").GetComponent<Image> ();
img.color = new Color(1.0F, 1.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==3)
{
Image img = GameObject.Find ("Image").GetComponent<Image> ();
img.color = new Color(0.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==0)
{
Image img = GameObject.Find ("Image2").GetComponent<Image> ();
img.color = new Color(0.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==1)
{
Image img = GameObject.Find ("Image2").GetComponent<Image> ();
img.color = new Color(0.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==2)
{
Image img = GameObject.Find ("Image2").GetComponent<Image> ();
img.color = new Color(0.0F, 0.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
if (countnum==3)
{
Image img = GameObject.Find ("Image2").GetComponent<Image> ();
img.color = new Color(0.0F, 1.0F, 0.0F, 1.0F); // (紅red,綠green,藍blue,透明)

}
}

void mywait()
{

while (WaitTimecount <= fMyWaitTime) {
//do stuff;

WaitTimecount = WaitTimecount + 0.01;
Debug.Log ( WaitTimecount );

}

}

}





2016年12月8日 星期四

2016年12月2日 星期五

物件導向C# - 繞圈跑

using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { Vector3[] T = new Vector3[4]; // Use this for initialization void Start() { T[0] = new Vector3(100f, 0f, 100f); T[1] = new Vector3(-100f, 0f, 100f); T[2] = new Vector3(-100f, 0f, -100f); T[3] = new Vector3(100f, 0f, -100f); } int aa = 0; // Update is called once per frame void Update() { transform.position = Vector3.MoveTowards(transform.position, T[aa], 50 * Time.deltaTime); if (transform.position == T[aa]) aa = (aa + 1) % 4; } }