| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5785 人关注过本帖, 1 人收藏
标题:已经下载python如何应用?
只看楼主 加入收藏
sssooosss
Rank: 9Rank: 9Rank: 9
等 级:禁止访问
威 望:3
帖 子:664
专家分:1115
注 册:2019-8-27
收藏
得分:0 
共同进步
2023-03-03 10:53
ysr2857
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:34
帖 子:809
专家分:77
注 册:2020-2-10
收藏
得分:0 
如下是用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版的可调用程序或叫可调用函数?
2023-03-17 09:25
ysr2857
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:34
帖 子:809
专家分:77
注 册:2020-2-10
收藏
得分:0 
while True:
    try:
        a = 2
        b = 0
        c = input('请输入一个数字:')
        c = eval(c)
        d=c**0.5
        e=int(d)
        for i in range(e):
            if c % a == 0:
                b = 2
                break
            if a < c:
                a += 1
        if b >= 2:
            print('这是一个合数')
        else:
            print('这是一个质数')
    except:
        print("错误")
        exit()
2023-03-18 16:30
ysr2857
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:34
帖 子:809
专家分:77
注 册:2020-2-10
收藏
得分:0 
学到一个语句,在python中用break可以跳出循环。
但是不知道如何做成可调用函数,并且调用这个函数?

欢迎各位老师和朋友沟通指导!谢谢!
2023-03-18 16:35
ysr2857
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:34
帖 子:809
专家分:77
注 册:2020-2-10
收藏
得分:0 
也可以使 range() 以指定数字开始并指定不同的增量(甚至可以是负数,有时这也叫做'步长'):

实例
>>>for i in range(0, 10, 3) :
    print(i)
 
   
0
3
6
9
2023-03-18 16:40
ysr2857
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:34
帖 子:809
专家分:77
注 册:2020-2-10
收藏
得分:0 
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()

[此贴子已经被作者于2023-3-18 19:07编辑过]

2023-03-18 17:33
ysr2857
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:34
帖 子:809
专家分:77
注 册:2020-2-10
收藏
得分:0 
请输入一个数字:997
这是一个质数
请输入一个数字:113
这是一个质数
请输入一个数字:2
这是一个质数
请输入一个数字:3
这是一个质数
请输入一个数字:5
这是一个质数
请输入一个数字:7
这是一个质数
请输入一个数字:111
这是一个合数
请输入一个数字:11111
这是一个合数
请输入一个数字:1111
这是一个合数
请输入一个数字:1111111
这是一个合数
请输入一个数字:11111111111
这是一个合数
请输入一个数字:1111111111111
这是一个合数
请输入一个数字:11111111111111111
这是一个合数
请输入一个数字:1111111111111111111
这是一个质数(这个19位的素数判断时间长了)
2023-03-18 19:18
东海ECS
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册:2023-1-24
收藏
得分:0 
回复 54楼 ysr2857
直接
break


就行了

会当凌绝顶,一览众山小.
2023-03-18 20:59
东海ECS
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册:2023-1-24
收藏
得分:0 
>>> list(range(1, 1000))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999]

会当凌绝顶,一览众山小.
2023-03-18 21:08
东海ECS
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册:2023-1-24
收藏
得分:0 
>>>import random
>>>list(random.randint(1, 1000) for i in range(1000))
[515, 777, 561, 599, 48, 210, 185, 134, 839, 870, 731, 59, 629, 334, 208, 429, 403, 778, 543, 345, 484, 805, 791, 705, 648, 144, 842, 391, 361, 493, 852, 807, 217, 40, 13, 482, 183, 423, 875, 49, 739, 566, 358, 735, 160, 860, 738, 259, 242, 396, 391, 32, 712, 810, 876, 146, 325, 122, 843, 643, 285, 649, 689, 143, 44, 131, 551, 437, 798, 369, 581, 541, 784, 712, 104, 248, 195, 416, 359, 717, 180, 59, 685, 983, 923, 753, 221, 520, 612, 691, 846, 72, 253, 720, 693, 835, 721, 810, 278, 45, 577, 674, 367, 961, 258, 256, 316, 667, 88, 678, 879, 159, 727, 309, 871, 848, 340, 902, 659, 127, 662, 39, 129, 952, 58, 205, 321, 373, 750, 569, 631, 618, 719, 11, 500, 744, 27, 53, 668, 851, 865, 489, 65, 532, 607, 24, 960, 992, 940, 169, 557, 529, 318, 71, 347, 201, 954, 68, 529, 833, 951, 138, 358, 508, 688, 741, 551, 530, 219, 972, 287, 329, 429, 664, 679, 445, 814, 968, 404, 541, 799, 470, 19, 764, 538, 364, 833, 340, 362, 814, 302, 861, 507, 788, 508, 429, 226, 254, 907, 755, 453, 461, 302, 571, 360, 414, 148, 641, 920, 343, 272, 43, 930, 868, 856, 497, 868, 562, 808, 700, 131, 384, 997, 229, 247, 375, 321, 99, 697, 184, 804, 873, 771, 146, 695, 801, 453, 768, 344, 157, 984, 479, 365, 272, 591, 968, 893, 301, 637, 859, 276, 34, 162, 904, 449, 159, 649, 874, 645, 983, 28, 526, 713, 979, 914, 593, 866, 480, 814, 727, 355, 140, 408, 532, 585, 508, 804, 373, 188, 511, 647, 548, 708, 707, 925, 765, 537, 230, 438, 948, 655, 639, 91, 43, 936, 36, 457, 138, 392, 582, 88, 171, 975, 709, 289, 980, 54, 190, 861, 101, 187, 331, 120, 597, 159, 496, 862, 42, 130, 314, 813, 905, 418, 693, 735, 849, 171, 793, 936, 471, 587, 183, 269, 711, 517, 186, 471, 927, 63, 300, 904, 436, 47, 579, 265, 899, 731, 963, 390, 512, 734, 398, 386, 569, 800, 821, 960, 387, 831, 8, 876, 573, 340, 583, 454, 173, 223, 706, 868, 109, 648, 480, 19, 912, 424, 45, 716, 321, 988, 907, 281, 147, 960, 901, 604, 896, 950, 135, 472, 990, 546, 989, 995, 169, 353, 361, 636, 39, 997, 612, 361, 538, 47, 346, 408, 872, 873, 935, 903, 171, 504, 828, 344, 524, 843, 404, 435, 748, 211, 770, 227, 901, 281, 536, 571, 399, 902, 393, 541, 585, 1, 813, 745, 351, 586, 285, 522, 916, 736, 484, 604, 99, 233, 55, 955, 733, 827, 808, 913, 751, 217, 26, 290, 561, 771, 732, 499, 362, 554, 400, 399, 532, 668, 587, 814, 246, 898, 382, 190, 27, 770, 434, 26, 702, 861, 602, 406, 216, 950, 575, 975, 449, 748, 220, 547, 936, 463, 650, 723, 625, 304, 827, 964, 592, 572, 361, 464, 180, 732, 804, 723, 402, 684, 512, 190, 914, 347, 856, 589, 869, 930, 334, 895, 249, 268, 109, 300, 983, 497, 428, 635, 72, 831, 381, 374, 885, 523, 598, 937, 690, 46, 995, 903, 590, 948, 258, 42, 190, 927, 445, 799, 266, 830, 70, 734, 545, 646, 870, 989, 322, 949, 533, 140, 140, 493, 182, 401, 458, 539, 282, 974, 145, 719, 240, 994, 786, 278, 288, 460, 123, 148, 737, 324, 787, 664, 965, 722, 303, 451, 231, 34, 318, 682, 49, 33, 556, 88, 505, 658, 282, 856, 415, 967, 862, 2, 327, 424, 20, 144, 536, 395, 547, 31, 201, 87, 484, 649, 205, 739, 63, 626, 836, 105, 387, 963, 727, 448, 298, 311, 18, 902, 195, 828, 989, 234, 354, 591, 146, 257, 898, 754, 728, 439, 331, 425, 941, 111, 794, 619, 662, 352, 374, 353, 397, 801, 467, 264, 636, 388, 586, 692, 596, 312, 68, 956, 453, 360, 103, 59, 889, 887, 184, 632, 952, 569, 744, 133, 345, 526, 342, 564, 361, 105, 269, 243, 848, 228, 172, 27, 252, 415, 72, 266, 491, 300, 585, 566, 30, 544, 328, 594, 724, 409, 209, 479, 981, 506, 516, 788, 840, 942, 88, 962, 975, 980, 139, 340, 885, 140, 24, 666, 272, 586, 515, 772, 483, 893, 643, 834, 581, 568, 461, 429, 472, 476, 747, 204, 751, 576, 355, 231, 820, 974, 4, 567, 926, 785, 548, 585, 119, 226, 415, 228, 55, 667, 186, 800, 846, 344, 302, 554, 986, 268, 402, 788, 485, 299, 986, 215, 497, 484, 909, 326, 486, 410, 382, 518, 663, 65, 82, 417, 689, 35, 683, 741, 363, 906, 624, 209, 814, 86, 683, 1000, 254, 829, 128, 851, 391, 176, 830, 344, 659, 931, 738, 375, 589, 824, 764, 286, 650, 135, 384, 722, 510, 244, 973, 902, 951, 790, 77, 609, 762, 95, 827, 38, 419, 864, 455, 923, 635, 887, 987, 697, 604, 709, 482, 332, 215, 179, 49, 798, 498, 529, 226, 947, 733, 663, 90, 521, 194, 728, 38, 156, 64, 895, 896, 524, 996, 699, 455, 783, 158, 533, 803, 11, 490, 924, 929, 270, 96, 939, 859, 284, 63, 38, 488, 977, 996, 388, 567, 957, 329, 975, 866, 445, 914, 472, 276, 742, 372, 977, 749, 843, 425, 840, 299, 49, 666, 995, 581, 70, 90, 824, 425, 758, 901, 396, 191, 193, 366, 694, 626, 685, 208, 588, 794, 58, 195, 705, 653, 266, 457, 75, 388, 460, 461, 973, 481, 204, 307, 122, 755, 710, 210, 112, 489, 809, 170, 356, 335, 632, 809, 218, 115, 46, 159, 709, 857, 782, 803, 510, 73, 48, 469, 596, 693, 73, 243, 719, 627, 204, 644, 743, 5, 351, 155, 736, 388, 450, 182, 77, 766, 561, 973, 589, 776, 160, 695, 856, 276, 312, 80, 867, 585, 252, 256, 816, 466, 942, 377, 921, 127, 261, 409, 964, 792, 883, 552, 188, 293, 311, 294, 845, 12, 851, 784, 396, 734, 537, 776]


生成随机数列表

会当凌绝顶,一览众山小.
2023-03-18 21:10
快速回复:已经下载python如何应用?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036695 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved