最新消息:

VB_Python代码对照算法百题(029)

Python 少儿编程 1498浏览 0评论

 VB代码:(2018年8月浙江名校新高考联盟)14.某网络服务平台,要求新注册的用户名必须以字母开头,由字母(区分大小写)、数字和下划线中的 6~18 个字符组成。编写一个 VB 程序,判断用户名的合法性,具体功能如下:在文本框Text1 中输入用户,单击 “检测”按钮,在标签 Text2 中输出结果。程序界面如图所示。

VB_Python代码对照算法百题(029)

(1)由下面代码可知运行界面中“检测”按钮的对象名是          

(2)实现上述功能的 VB 程序如下,请在划线处填入合适的代码。

Private Sub Btn_Click()

Dim s As String, n As Integer, i As Integer

Dim n1 As Integer, n2 As Integer, n3 As Integer

s = Text1.Text

            

ch = Mid(s, 1, 1)

If n < 6 Or n > 18 Then Text2.Text = “不合法”: Exit Sub ‘退出当前子程序

If Not (ch >=”a” And ch <= “z” Or ch >= “A” And ch <= “Z”) Then             Text2.Text = “不合法”: Exit Sub

End If

For i = 2 To n

    ch = Mid(s, i, 1)

    If ch >= “a” And ch <= “z” Or ch >= “A” And ch <= “Z” Then

         n1 = n1 + 1

    ElseIf ch >= “0″ And ch <= “9″ Then

        n2 = n2 + 1

    ElseIf ch >= “_” Then

        n3 = n3 + 1

    End If

Next i

If           Then Text2.Text = “不合法” Else Text2.Text = “合法”

End Sub

(3)若文本框 Text1 中输入的内容为“XWang”,点击 “检查”按钮后,标签 Label2 中显示的内容是        

参考答案:

(1)Btn (不区分大小写)  (1分)

(2) ①n = Len(s) 或n = Len(Text1.Text)   (1分)

n1 + n2 + n3 < n-1 or n2 = 0 or n3 = 0     (2分)

或 n1 + n2 + n3

 

(3)不合法

 

Python参考代码:

n1=0

n2=0

n3=0

flag=True

while flag:

    s=input(‘请输入用户名:n’)

    n=len(s)

    ch=s[0]

    if 18>n>6:

        flag=False

    if ‘z’>=ch>=’a’ or ‘Z’>=ch>=’A’:

        flag=False

for i in range(1,n):

    ch=s[i]

    if ‘z’>=ch>=’a’ or ‘Z’>=ch>=’A’:

        n1=n1+1

    elif ’9′>=ch>=’0′:

        n2=n2+1

    elif ch==’_’:

        n3=n3+1

if n1 + n2 + n3 < n-1 or n2 * n3 == 0:

    print(‘结果:’,’不合法’)

else:

    print(‘结果:’,’合法’)

VB_Python代码对照算法百题(029)

python代码运行结果:

VB_Python代码对照算法百题(029)

 

您必须 登录 才能发表评论!