day2_Python basics 2

1. Formatted output

1、%s or %d

%: Represents a placeholder. Note that when % needs to be displayed in the content, add a % before it to escape. For example, if 5% is displayed, then: 5%%

s: represents a string

d: represents the value digital

example:

name = input( ' Please enter your age ' )
age = input( ' Please enter the age ' )
height = input( ' Please input height ' )
msg = " My name is %s, this year is %s, my height is %s, and my learning progress is 3%%s " % (name, age, height)   #   In 3%%s, the first % is the suffix 
print (msg )
name = input( ' Please enter name ' )
age = int(input( ' Please enter your age ' ))
job = input( ' Please enter a job ' )
hobbie = input( ' your hobby ' )

msg = '''------------ info of %s -----------
Name  : %s
Age   : %d
job   : %s
Hobbie: %s
------------- end ----------------- ''' % ( name, name, age, job, hobbie)   # Note that there are two value
print(msg)

2. Initial coding

1. ASCII: 4 binary bits form one byte, one ASCII code occupies one byte

2, Unicode: occupy 4 bytes, too much space.

3. utf-8: an upgraded version of Unicode

Use at least 1 byte to represent English

2 bytes for European text

Occupies 3 bytes to represent Chinese

 

三、while - else

When the while is interrupted by break, do not go else

 

Fourth, data conversion

number -- bool value

Non-zero number --> True; zero --> False

True --> 1; False --> 0

Five, logical operation

()not and or

Operation priority: () > not > and > or

 

Operation

1. Write code: Calculate the sum of all numbers except 88 in 1 - 2 + 3 ... +(-) 99?
1  # 8. Write the code: Calculate the sum of all numbers in 1 - 2 + 3 ... + 99 except 88? 
2  
3 count = 1
 4 sum = 0
 5  while count < 100 :
 6      if count == 88 :
 7          count = count + 1
 8          continue 
9      else :
 10          if count % 2 == 1 :
 11              sum += count
 12          else :
 13              sum -= count
 14      count = count + 1 
15 print (sum)
1  #Write   code: Calculate the sum of all numbers except 88 in 1 - 2 + 3 ... + (-) 99? 
2  
3 i = 0
 4 j = -1
 5 sun = 0
 6  while i < 00 :
 7      i += 1
 8      j = - j
 9      if i == 88 :
 10          continue 
11      else :
 12          # j = -j # will The negative sign corresponding to 88 is also skipped, and it is carried over to the next 
13          sun = sun + i* j
 14  print (sun)

 

2. The user logs in (three chances to make mistakes) and displays the number of remaining mistakes each time he makes a mistake (hint: use string formatting)

1 username = " yangxiaoer " 
2 password = " 123456 " 
3 i = 3
 4  while i > 0:
 5      zh = input( " Please enter your account number: " )
 6      i -= 1
 7      if zh == username:
 8          mm = input( " Please enter your password: " )
 9          if mm == password:
 10              print ( " Authentication successful. Logging in... " )
 11              print (''' Congratulations on your successful login!
 12Welcome              users to enter
 13Username              :%s
 14Password              :%s
 15              ''' % (zh, mm))
 16              break 
17          else :
 18              if i == 0:
 19                  print ( " Your chance is gone! game over, see you next time! " )
 20                  answer = input( ' Try again? Y or N ' )
 21                  if answer == ' Y ' :
 22                      i = 3
 23              print(" Wrong password, please re-enter " )
 24              print ( " You still have a chance " +str(i)+ " a chance " )
 25      else :
 26          print ( " Please enter the correct username! " )
 27          if i == 0:
 28              print ( " Your chance is gone! " )
 29              answer = input( ' Try again? Y or N ' )
 30              if answer == ' Y ' :
 31                 i = 3
 32          print ( " you still have " + str(i) + " a second chance " )
 33  else :
 34      print ( ' you TM shameless ' )

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325384930&siteId=291194637