Python Introductory Exercises 2

Source: Internet
Author: User

1, making forms

Loop Prompt user input: User name, password, mailbox (requires user input longer than 20 characters, if more than the first 20 characters are valid)

If the user enters Q or Q to exit the program, the user input is displayed in a tabular format

User_input = None #用户名的初始值pwd = None #密码的初始值email = None #邮箱地址的初始值public = None #公共变量名初始值flag = False #标志位while Not flag: #当flag不为False时, execute the following code block print (' Warm reminder: If you need to exit the program, press "Q" or "Q" to exit ') public = input (' Please enter your user name: ') #提示用户输入用户名 if Le N (public) <=: #但是用户输入的长度小于或者等20个字符时, first do nothing else:public = public[:20] #但是用户输入的长度大于20个字符 , the first 20 characters in the string are taken if Public.lower ()! = ' Q ': #如果用户输入的字符串中不包含q时, the value entered by the user is assigned to user_inpput user_input = Public Else     : Flag = True #如果用户输入的字符串中包含q时, the flag tag bit is true, the user enters the string containing Q when the user enters Q can exit the program break public = input (' Please enter your password: ') If Len (public) <= 20:pass else:public = public[:20] If public.lower ()! = ' Q ': pwd = pu Blic Else:flag = True Break public = input (' Please enter your e-mail address: ') If Len (public) <= 20:pass E Lse:public = public[:20] If public.lower ()! = ' Q ': email = public Else:flag = True Bre Akuser_output = "' name\tpassword\temail%s\t%s\t%s "% (user_input,pwd,email) print (user_output.expandtabs) ######### #结果输出 ########### # # # #温馨提醒: If you need to quit the program please press "Q" or "Q" to exit Please enter your user name: laiying Please enter your password: 123 Please enter your email address: [email protected] Warm reminder: If you need to exit the program, press "Q" or "Q" Exit Please enter your username: QName password emaillaiying 123 [email protected]

2. Implement an integer addition calculator

such as: content = input (' Please enter content: ') #5 +9 or 1+3

1SUM1 = 0#the initial value of the sum2User_input = input ('Please enter the number you want to calculate:'). Split ('+')#prompts the user for input and splits the user's input by the + sign3  forIteminchUser_input:#iterate over each element entered by a user4     ifItem.isdigit ():#If the user enters a number5user_input = Int (item)#converts a user-entered number to an int type and assigns the converted value to User_input6Sum1 + = User_input#each time the user iterates, the elements entered by the user are added once, and so on7     Else:#If the user enters a message that is not an integer, give the user a hint and exit the program8         Print('you are not entering a number, the program exits')9          BreakTen Else: One     Print(SUM1)#to print a sum value A  - ################## #结果输出 ################## -  the #Please enter the number you want to calculate: 6+10 - # -

Python Introductory Exercises 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.