Python Learning-Basic-day1

Source: Internet
Author: User

In mid-April 17, I signed up for the old boy Python Advanced automation Development Course in 51cto, which is a meaningful thing to record in learning (sense of self)

First, the basic configuration and use of Pycharm

Before self-study for a period of time, has been used with the own idle and notepad++, now started with Pycharm, but very unfamiliar with the use of constantly supplemented and modified

Start to set fonts, templates and so on, the internet is easy to search

1.setting--editor--file and code template, find the Python script, you can define a template for the new Python file, only the new one will take effect.

2. Common shortcut keys:

Ctril + D Copy as Forward

Ctril + Y Delete when moving forward

tab to add a tab to the current selection, instead SHIFT + Tab

Ii. Basic points of knowledge

2.1 input () The default input is the string format, if the input number needs to compare size, can be int (input ())

2.2 Python 2.x If you need to use Chinese, you need to declare #-*-Coding:utf-8-*-because ASCII is used by default

2.3 Continue and break

forinrange(10):

     if i< 5 :          continue #不往下走了,直接进入下一次loop      print ( "loop:" , i ) for in range ( 10 ):      if i> 5 :          break #不往下走了,直接跳出整个loop      print ( "loop:" , i ) Three, while, for loop example:

Guess age, guess right, jump out of the entire cycle, a total of 3 opportunities, are wrong, the hint whether continue, no, direct launch. Three examples were thought of in terms of context:

The first one is on the video, the most concise
Cq_age = 27
n = 0
While n < 3: #只要n小于3, that is, 0,1,2, three times will execute while, otherwise the last else is executed.
N +=1
age = Int (input ("Please enter your guess:"))
If age < cq_age:
Print ("Think bigger")
Elif Age > Cq_age:
Print ("Thinker smaller")
Else
Print ("You Got It")
Break #跳出整个while循环, which belongs to the non-normal execution while, so the last else is not executed
if n = = 3:
Answer = input ("Do wangt conntinue?")
If answer! = ' n ': #只要你不回答n, the default agrees to continue, so that the variable n is assigned a value of 0 again, and if it is N, then n==3, the loop is false, and the last else is executed.
n = 0
Else
Print ("You have tried 3 times, fuck off")
The second one is not watching the video before, self-pondering, good low:
Cq_age = 27
n = 0
While n = = 0: #大循环, n as long as 0 is true, this patrol has been executed
While n < 3: #小循环
N +=1
age = Int (input ("Please enter your guess:"))
If age < cq_age:
Print ("Think bigger")
Elif Age > Cq_age:
Print ("Thinker smaller")
Else
Print ("You Got It")
Break #break会跳出当前的循环, here only to jump out of the small loop, so the next if statement to determine the age of guessing, continue to break cycle, end, or just jump out of the small loop, cycle n equals 0 and then do not meet the conditions to perform the last else, not required
Else
Answer = input ("You have tried 3 times, does you want continue?")
If answer! = ' n ':
n = 0 #保证大循环的n等于0条件仍然满足, continue cycle once, then the small loop will continue to execute
#else:
# Break
if age = = Cq_age: #对应上面的第一个break,
Break
Else
Print ("Bye, Lowerb")
for writing.
Cq_age = 27
n = 0
While True: #试了3次且答复不继续或答对了才会退出, otherwise the loop has been
For n in range (3):
age = Int (input ("Please enter your guess:"))
If age < cq_age:
Print ("Think bigger")
Elif Age > Cq_age:
Print ("Thinker smaller")
Else
Print ("You Got It")
Break
Else
Answer = input ("You tried 3 Times,do want continue?")
If answer = = ' n ':
Print (' Bye ')
Break #此break对应的是紧邻的else, not within the for loop, so jumping out of a while loop
if age = = Cq_age:
Break #也是跳出while循环

Iv. Homework: (Call the file forget, first write the answer logic)

4.1 Writing the Login interface

    • Enter User name password
    • Show welcome message after successful authentication
    • After the wrong three times the lock (that is, after the lock, then log in, the user is prompted to lock. So you have to do a lock check before each landing.

General idea: User name: The password exists in a dictionary user_pass, the locked user exists a list lock_user, after entering the user name, check whether the user name in Lock_user, in the words, the prompt is locked, exit; If there is no lock, check if the user name is

In the dictionary, not in the words prompt invalid user name, in the words and then enter the password, verify three times unsuccessful, add the user to the Lock_user list. These tables are then replaced with files, which are read before each run.

User_pass = {' Cui qing ': ' abc123 ', ' Zhangsan ': ' abc111 ', ' Lisi ': ' aaa123 '}
Print (user_pass[' Cui Qing ')
Lock_user = [' Zhangsan ', ' Lisi ']
Print (Lock_user)
n = 0
While True:
Username = input ("Please enter your username:")
If username in Lock_user:
Print (' Your account is locked! ')
Break #退出while True Loop
Elif username in User_pass:
Print (' Your account is true ')
While n < 3:
n + = 1
Password = input (' Enter your password: ')
if password = = User_pass[username]:
Print ("Welcome%s"%username)
Break #退出小循环
Else
Print ("Your password is wrong")
Else
Print ("'
Wrong password
Your account is locked.
‘‘‘)
Lock_user.append (username)
Print (Lock_user)
Break #退出while True Loop
if password = = User_pass[username]:
Break #退出while True Loop
Else
Print (' Invalid username ')

Python Learning-Basic-day1

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.