PYTHON
the roadSeven
the basic if judgment
Simplest process handling: If ... else
If Simple exercise:
#!/usr/bin/env python
#-*-coding:utf-8-*-
#if Basic Expressions
# User Login Verification
# Prompt for user name and password
# Verify user name and password
# If the error, the output user name or password error;
# If successful, then output welcome , XXX!
user = ' Zhang '
Password = ' 123.com '
Username = input ("Username:")
Password = input ("Password:")
If user = = Username:
Print (" user name entered correctly ") { downlevel code }
Lower code:
This code is run to rely on the previous layer of code, that is, the previous layer of code must be True (set) before reading the underlying code. /// can be understood as a sub-code of the previous layer
The child code cannot write in the top row, otherwise Python would consider it to be irrelevant code and would be executed independently
Python 's default development specification: Child code empty 4 grid
The If ... else statement exercises:
#!/usr/bin/env python
#-*-coding:utf-8-*-
user = ' Zhangjianghua '
passwd = ' 123.com '
Username = input ("Username:")
Password = input ("Password:")
If user = = Username:
Print ("username is correct ...")
if password = = passwd:
Print ("Welcome login ...")
Else
Print ("Password is invalid ...")
Else
Print (" even the user name is not right, roll the coarse ... ")
If...else V2 optimized version:
#!/usr/bin/env python
#-*-coding:utf-8-*-
user = "Zhangjianghua"
passwd = "123.com"
Username = input ("Username:")
Password = input ("Password:")
if user = = Username and passwd = = Password:
Print ("Welcome login")
Else
Print ("Invalid username or password ....")
Eight, guess the age game
(guess the age game V1, guess right or quit)
#!/usr/bin/env python
#-*-coding:utf-8-*-
Age = 19
guess_num = Int (input ("Input your guess num:"))
If Guess_num ==age:
Print ("congratulations! You got it. ")
Elif Guess_num >age:
Print ("Think smaller!")
Else
Print ("Think Big ...")
Nine, for Loop
For I in range (10):
Print (i)
I i will increase by 1 per cycle
( guess age game V2, Max three times )
#!/usr/bin/env python
#-*-coding:utf-8-*-
Age = 19
For I in range (10):
If I <3:
guess_num = Int (input ("Input your guess num:"))
If Guess_num ==age:
Print (" Congratulations on your guess ")
Break # Guess right stops, doesn't go backwards, jumps out of the loop
Elif Guess_num >age:
Print (" Think small!" ")
Else
Print (" Think Big!" ")
Else
Print (" tried too many times . Bye ~~")
Break
(Guess the Age game V3, maximum cycle ten times, every 3 cycles )
#!/usr/bin/env python
#-*-coding:utf-8-*-
#Author: Zhangjianghua
Age = 19
Counter =0
For I in range (10):
Print ('--counter: ', counter)
If counter <3:
guess_num = Int (input ("Input your guess num:"))
if Guess_num = = Age:
Print (" Congratulations, you guessed it!") ")
Break # Don't go backwards, jump out of the loop
Elif Guess_num >age:
Print (" go to the small guess!" ")
Else
Print (" big guess!") ")
Else
Continue_confirm = input (" do you want to continue?") y|n ")
if continue_confirm = = ' Y ':
Counter = 0
Continue # ( jump out of the loop, not the whole )
Else
Print ("Bye")
Break
Counter +=1 #counter = Counter +1
Python the third day