Python variables, character Exercise 1

Source: Internet
Author: User
Tags first string greatest common divisor

1. determine whether a variable is legitimate: (a variable consists of a letter, a numeric underline; and the beginning is not a number)

While True:

s = raw_input ("Please input values:")

If s== "Exit":

Print "Welcome use nexttime!"

Break

If S[0].isalpha () or s[0] = = "_":

For i in S[1:]:

If not (I.isalnum () or I = = "_"):

Print "%s variable name is not valid!"%s

Break

Else

Print "%s variable name is valid!"%s

Second, input Word Reverse output (Xiaomi written test)

Sentenct = raw_input ("Please input sentenct:")

A = Sentenct.split ("")

Print "". Join (A[::-1])

Third, enter two strings to remove all characters from the second string (example: input: Hello World output: World Hello)

char1 = raw_input ("Please input first string:")
char2 = raw_input ("Please input second string:")
For i in CHAR1:
if I in Char2:
Continue
Else:
print "%s"% i,

Four, The user enters an integer number to find the factorial of the number

num = int (raw_ Input ("Enter integer:"))
rest = 1
Span style= "FONT-FAMILY:CALIBRI; Font-size:14pt ">for i in range (1, num + 1):
     Rest *= i
print " output%d factorial:%d " %  (num, rest)

Five, user Login, and no more than three times

For I in range (3):
username = raw_input ("Please input username:")
Password = raw_input ("Please input password:")
if username = = "root" and password = = "Westos":
print "Login success!!"
Break
Else:
print "Login error!!"
print "You have%d times changes"% (2-i)
Else:
print "More than three times! Try again after 10s "

Six, Determine the absence record of a student (no more than one a and no two consecutive L rewards)

While True:
record = Raw_input ("Please enter the student's absence record:")
print Record.count ("LLL") = = 0
if Record = = "Exit":
print "Exit ... "
Break
if (Record.count ("A") <= 1 and Record.count ("LLL") = = 0):
print "There are rewards!" "

Else:
Print "No Rewards!!" "

Seven, enter a line of characters, statistics how many words, every two words separated by empty reform
Line = raw_input ("Please input sentence:")
li = line.split ()
b = Len (LI)
print "There is%d words in the line"% (b)
Eight, the number of characters in the request list
char = raw_input ("Please input sentence:")
print len (char)

Nine, whether it is a palindrome number
num = raw_input ( "Please input values:")
Span style= "FONT-FAMILY:CALIBRI; Font-size:14pt ">if  (Num == num[::-1]):
& nbsp;   print  "is a palindrome number! "
else:
    print " is not a palindrome number "

Ten, beg greatest common divisor and least common multiple

min_num = min (num1, num2)
for I in range (1, Min_num + 1):
if (num1% i = = 0 and num2% i = = 0):
Gys = i
GBS = NUM1 * Num2/gys
print "%d and%d greatest common divisor:%d"% (NUM1, num2, Gys)
print "%d and%d Max public:%d"% (NUM1, num2, GBS)

Xi. guessing number games

Import Random
Systom = Random.randint (1, +)
Print Systom
num = 1
While (Num <= 5):
Guest = Int (raw_input ("Please enter the number you guessed (1-100):"))
if Guest < systom:
print "Too small!!"
elif Guest > Systom:
print "Too big!!"
Else:
print "congratulations!!"
Break
num + = 1

12. performing operations on a terminal

Import OS # can execute system commands
While True:
cmd = raw_input ("[[email protected] ~]$")
if cmd:
if cmd = = ' exit ':
print "Logout"
Break
Else:
#print ' run%s '%cmd
os.system (cmd)
Else:
Continue

Python variables, character Exercise 1

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.