Some basic exercises for the Python loop

Source: Internet
Author: User

#1:
name = input (' Please enter your identity ')
if name = = ' Egon ':
print ('--super admin ')
elif name = = ' Tom ':
print ('--general admin ')
elif name = = ' Jack,rain ':
print ('-to Business executives ')
elif Name = = ' other ':
print ('--normal user ')


#2
Today = input (' What's the day of the week? ')
if today = = ' Monday ' or today = = ' Tuesday ' or today = = ' Wednesday ' or today = = ' Thursday ' or today = = ' Friday ':
print (' work ')
elif today = = ' Saturday ' or today = = ' Sunday ':
print (' Out of the Waves ')


#3,
(1). Using while loop output 1 2 3 4 5 6 8 9
a=1
While a <11:
print (a)

# (2). Ask for 1-100 of all numbers and
sum=0
Count =1
While count <=:
sum + = Count
count + = 1
print (sum)

# (3). All odd numbers in the output 1-100
sum=0
Count =1
While count <=:
sum + = Count
count + = 2
print (sum)

# (4). All even numbers in the output 1-100
sum1=0
count1 =0
While count1 <=:
sum1 + = Count1
Count1 + = 2
print (SUM1)

# (5). Beg 1-2+3-4+5 ... 99 of all numbers of the and
# sum2=0
count2 = 0
While Count2 <100:
if COUNT2/2 = = 0: #偶数
sum + = Count2
elif COUNT2/2 = = 1: #奇数
Sum-= Count2
print (sum2)

#6. User Login (three chance retry)
Count3 =0
While Count3 <3:
user_name=input (' Please enter user name ')
pwd=int (Input (' Please enter password '))
if user_name = = ' Boss ' and pwd = = 123:
print (' login successful ')
Break
Else:
print (' Login failed, please re-enter: ')

#7: guess Age game
#要求: Allow the user to try up to 3 times, 3 times without guessing the right, then directly exit, if guessed right, print congratulations message and exit
b1=0
While B1 <3:
Age = Int (input (' Please enter password '))
if Age = =:
print (' login successful ')
Break
Else:
print (' Login failed, please re-enter: ')

#8: Guess the Age game upgrade version
#要求:
"allows users to try up to 3 times
after each attempt 3 times, if not guessed right, ask the user whether still want to continue to play, if answer Y or Y, continue to let it guess 3 times, to this reciprocating,
If you answer n or n, you exit the program how to guess the right, and then exit directly .
b2=0
While True:
if b2 = = 3:
Jixu = input (' You have guessed the wrong 3 times, do you still want to guess? y/n ')
if Jixu = = ' Y ':
b2 = 0
elif Jixu = = ' N ':
print (' exit successful ')
Break
Else:
print (' invalid operation, please re-operate! ')
count = 3 # Input Other non-y/n string, return to continue selection guess or not
Continue
age = Int (input (' ask you to guess ages '))
if Age = =:
print (' Congratulations, guess the age! ')
Break
Else:
print (' Wrong, please re-enter ')
B2 + = 1


#4
#编写计算器程序, requirements
1, user input quit quit the program
2, the program runs, let the user select the specific calculation operation is the addition or multiplication or division ... Then enter a number to perform the Operation
3, simple demonstration as follows, can be improved on this basis
msg= ""
1 Addition
2 Subtraction
3 Multiplication
4 Division
" "
Print (msg)
choice = input (' Please select the symbol (1-4) to be operated on: '). Strip ()
NUM1 = input (' Enter the first number to be calculated: '). Strip ()
num2 = input (' Enter a second number to be calculated: '). Strip ()
if choice = = ' 1 ':
res=int (NUM1) +int (num2)
print ('%s+%s=%s '% (num1,num2,res))
if choice = = ' 2 ':
res = int (NUM1)-int (num2)
print ('%s-%s=%s '% (NUM1, num2, res))
if choice = = ' 3 ':
res = int (NUM1) * INT (num2)
print ('%s*%s=%s '% (NUM1, num2, res))
if choice = = ' 4 ':
res=int (NUM1)/int (num2)
print ('%s/%s=%s '% (num1,num2,res))


#5, nested implementation based on for loop
5.1 printing 99 multiplication Table
For i in range (1,10):
For J in Range (1,i+1):
print ('%s*%s=%s '% (j,i,i*j), end= ') #九成九法则
print ()

#5.2 Printing Pyramids
y = 5
For x in range (1,y+1):
For i in range (y-x):
Print (', end= ') # Prints multiple spaces consecutively in a row
For J in Range (2*x-1):
print (' * ', end= ') #在一行中连续打印多个空格
print ()
print ('----------delimiter-------------')
"' #max_level =5
********* #current_level = 1, number of spaces =0,* =9
******* #current_level = 2, number of spaces =1,* =7
* * * * * #current_level = 3, number of spaces =2,* =5
* * * #current_level = 4, number of spaces =3,* =3
* #current_level = 5, number of spaces =4,* =1
#数学表达式
# Number of spaces =max_level-current_level spaces =current-1
# * # =2*current_level-1 * # =2*max_level-1 "
y=5
For x in range (1,y+1):
For i in range (x-1):
print (', end= ')
For J in Range (Y-x) +1:
print (' * ', end= ')
print ()

Some basic exercises for the Python loop

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.