c语言转python急急急

Python019

c语言转python急急急,第1张

import random

def player(tol):

    inp = 100

    while inp > 2 or inp < 1 or inp + tol > 50:

        inp = int(raw_input("Please count (1 or 2): "))

    return inp + tol

def computer(tol):

    if tol + 1 == 50 or (tol + 1) % 3 == 0:

        tol += 1

    elif tol + 2 == 50 or (tol + 2) % 3 == 0:

        tol += 2

    else:

        tol += int(random.random() * 2) + 1

    print("Computer count: {}".format(tol))

    return tol

def main():

    tol = 0

    print("* * * * * * * * Grab Fifty * * * * * * * *")

    print("Game Begin")

    whose_turn = player

    if random.random > .5:

        whose_turn = computer

    while tol != 50:

        tol = whose_turn(tol)

        if tol == 50:

            if whose_turn == player:

                print("You Win!")

            else:

                print("You Lose!")

            break

        whose_turn = player if whose_turn == computer else computer

    print("* * * * * * * * Game Over * * * * * * * *")

if __name__ == "__main__":

    main()

要将C语言转化为python,前提:

1 对C语言语法即python编程均熟练掌握;

2 对算法相关有一定了解;

3 至少可以看懂要转换的C语言程序。

由于C语言的语句,与python没有对应关系,所以只能按照如下方式转换:

1 读懂C语言代码实现功能,可以以函数为单位;

2 按照功能,依照python方式实现相同功能。

可以的。

C中内嵌Python

新建立一个工程,首先需要将工作目录设置到Python-3.1.1PCbuild中,以获取到动态库,至于静态库的包含,Include目录的指定,那自然也是少不了的。文件中需要包含Python.h文件,这也是必须的。

接口中

Py_Initialize()

Py_Finalize()

其他的根据需求,再引入相应的python builder 即可