注册 登录
编程论坛 VB6论坛

VB6.0可调用程序如何转化为python可调用函数?

ysr2857 发布于 2023-03-17 10:01, 1172 次点击
如下是用VB6.0编程的快速判断素数的可调用程序:
Private Function fenjieyinzi(sa As String) As String
Dim x, a, b
x = sa
b = Int(Sqr(Val(x)) / 2)
If x = 3 Or x = 2 Then
a = True
Else
If x Mod 2 = 0 Then
a = False
Else

For I = 3 To 2 * b + 1 Step 2
If x Mod I = 0 Then
a = False
Exit For

Else: a = True

End If
Next
End If
End If
If a = True Then
fenjieyinzi = "这是个素数"
Else
fenjieyinzi = "2*2"
End If

如何转化成python版的可调用程序或叫可调用函数?
6 回复
#2
ysr28572023-03-18 23:16
while True:
    try:
        a = 2
        b = 0
        c = input('请输入一个数字:')
        c1=c[-1]
        c = eval(c)
        d=c**0.5
        e=int(d)
        if c == 2:print('这是一个质数')   
        elif eval(c1) % 2 == 0 :
               print('这是一个合数')
        else :
               for i in range(3,e,2):
                 if c % i == 0:
                    b = 2
                    break
                 
               if b >= 2:
                    print('这是一个合数')
               else:
                    print('这是一个质数')
    except:exit()

不会做可调用函数,要求输出a=可调用函数的值或结果
#3
apull2023-03-19 11:59
程序代码:

import math


def sushu(x: int):
    if x == 3 or x == 2:
        return True

    if x % 2 == 0:
        return False

    b = int(math.sqrt(x) / 2)

    for i in range(3, 2 * b + 1, 2):
        if x % i == 0:
            return False

    return True


c = int(input('请输入一个数字:'))
if sushu(c):
    print(f"{c}是素数")
else:
    print(f"{c}不是素数")

#4
ysr28572023-03-19 15:37
回复 3楼 apull
谢谢老师!非常感谢,这样的程序无法运行,格式太难弄了,缩进量弄不对呀?
#5
ysr28572023-03-19 15:42
回复 3楼 apull
>>> import math
>>> def sushu(x: int):
    if x == 3 or x == 2:
        return True

    if x % 2 == 0:
        return False

    b = int(math.sqrt(x) / 2)

    for i in range(3, 2 * b + 1, 2):
        if x % i == 0:
            return False

    return True


    c = int(input('请输入一个数字:'))
    if sushu(c):
        print(f"{c}是素数")
    else:
        print(f"{c}不是素数")

        
>>> 不工作啊,咋回事?编译器的过吗?
#6
mrexcel2023-03-19 16:49
编辑器可以下载Jupter 或者Spyder 或者Sublime Text培养或者Pycharm
#7
ysr28572023-03-20 07:00
回复 6楼 mrexcel
谢谢老师!可能是有,我不懂不会用,继续学习吧!谢谢您!
1