Python learning-The use of simple interactive raw_input

Source: Internet
Author: User
Tags ord

Use of Row_input:

>>> name=raw_input ("Please input your name:") please input your name:xiaobai>>> name ' Xiaobai '

Write a small program that asks for the user's name, gender, age, job, salary, and output in a formatted manner:

Information of company stuff:

Name:

Age:

Sex:

Job:


Code:

[Email protected] ~]# vim information_of_stuff.py #!/bin/pythonname=raw_input ("Please input your name:") age=raw_input ("Please input your-age:") sex=raw_input ("Please input your-sex:") job=raw_input ("Please input your job:") print "" ======= =======================information of company Stuff:name:%sage:%ssex:%sjob:%s============================== "% ( Name,age,sex,job)

Perform:

[email protected] ~]# python information_of_stuff.pyplease input your name:xiaobaiplease input your age:25please input yo ur sex:maleplease input your job:engineer==============================information of company Stuff:Name:xiaobaiAge: 25sex:malejob:engineer==============================

Enter a 0~100 direct number for the user to guess, and the number of guesses cannot be greater than three times.

[[email protected] ~]# vim guess_num.py   #!/bin/pythonimport  Osos.system (' Clear ')                     #执行时先清屏real_num =int (raw_input ("please input the real_num from 0 &NBSP;TO&NBSP;100: ") os.system (' Clear ')                     #输入让用户猜的数字后清屏retry_count =0                         #设定循环关闭条件while  retry_count<3:                  #后面加冒号         guess_num=int (raw_input ("Please input &NBSP;A&NBSP;NUMBER&NBSP;FROM&NBSP;0&NBSP;TO&NBSP;100: "))         if  guess_num>real_num:                print  "Wrong!  please try smaller! "                 retry_count+=1         #自增         elif guess_ num<real_num:      #多个条件用elif                  print  "wrong! please try bigger!"                 retry_count+=1         else:                          #最后一个条件用                  print  "ConguratioNs! you got it! "                 break                  #跳出循环else:         print  "Too much times!"

Unlike the shell, Python does not have a fi loop close symbol, but is indented to control the level of code, the same level of code indentation should be consistent, if and else is not the same level, indentation is different can also be executed, but not in line with the writing specification.

Raw_input input is a string, and the string is automatically converted to an ASCII value when compared to a number, so you use int to convert it to an integer type and break to jump out of the loop.

Ord: Converts a string to a value corresponding to ASCII.

>>> Print ord ("a") 97>>> print ord ("1") 49

Optimization code, the above code input enter or string will be error, and the number is not random value, need to optimize.

[[Email protected] ~]# vim guess_num.py   #!/bin/pythonimport osimport  randomos.system (' Clear ') real_num=random.randrange (+) Os.system (' Clear ') retry_count=0while retry_ Count<3:        guess_num=raw_input ("Please input a  NUMBER&NBSP;FROM&NBSP;0&NBSP;TO&NBSP;100: "). Strip ()   #去空格回车          if len (Guess_num) ==0:            # Determines whether the string length is 0                 Continue        if guess_num.isdigit ():            #判断是否全为数字                  guess_num=int (Guess_num)          else:                print  "You should input  a number instead of string! "                 continue                   #跳出当前循环, Make the next cycle         if guess_num>real_num:                 print  "wrong! please  try smaller! "         elif guess_num<real_num:                 print  "Wrong! please try  bigger! "         else:                 print  "Congurations! you got the real number %d ! " %real_num                break         retry_count+=1else:         print  "Too much times! the real number is", Real_num

The. Strip () indicates that the input spaces and carriage return are removed;

Len (guess_num) indicates the length of the computed string;

Continue means to jump out of the current loop and make the next loop;

IsDigit () Indicates whether the judgment is full of numbers;


Change the above loop to a for loop:

[[Email protected] ~]# vim guess_num_for.py    #!/bin/pythonimport  osimport randomos.system (' Clear ') real_num=random.randrange (+) Os.system (' Clear ') for i in  range (3):         guess_num=raw_input ("Please input a &NBSP;NUMBER&NBSP;FROM&NBSP;0&NBSP;TO&NBSP;100: "). Strip ()         if  len (Guess_num) ==0:                 continue        if guess_num.isdigit ():                 guess_num=int (Guess_num)         else:                 print  "you should input a number  instead of string! "                 continue         if guess_num>real_num:                 print  "wrong! please try  smaller! "         elif guess_num<real_num:                 print  "Wrong! please try  bigger! "         else:                 print  "congurations! you got the real  number %d ! " %real_num                 breakelse:        print  "Too much times!&nbsP The real number is ", Real_num

Range is an array whose arguments are the starting value, the end value, and the step size, respectively.

>>> range [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> Range (2,10,2) [2, 4, 6, 8]>>> Range (2,20,5) [2, 7, 12, 17]

The design of a multi-layer loop, the user entered the correct password to enter the directory to select, and can exit the loop.

[[email protected] ~]# cat mulit_loop.py    #!/bin/pythonpasswd= "Test] logout=false             #加跳出的flagfor  i  In range (3):     password=raw_input ("Please input your password:"). Strip ()     if len (password) ==0:         Continue    if password==passwd:        print   "welcome to login!"         while True:             user_select=raw_input ("             ====================================             please input a number to continue            1.send files;             2.Send emalis;             3.exit this level;             4.exit the whole loop.             ====================================              "). Strip ()             user_select=int (user _SELECT)             if user_select==1:                 print  "Sending  files as you wish! "             if user_select==2:                print  " sending emails as you wish! "             if user_select==3:                 print  "Exit this  level,please re-input the password! "                 break             if user_select==4:                 print  "Ok, let ' s  have a break! "                 logout=true                 break         if logout==true:             break


Python learning-The use of simple interactive raw_input

Related Article

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.