Python learning [For Loop]

Source: Internet
Author: User

For loop
The for variable in can iterate over the object:
Loop body (break, continue)
Else
Execute else when loop is over

Practice:

3.3. Make the. While and for loops print each element of the string s= "Asdfer", respectively. 

#1. While Loop
#
s= "Asdfe"
# count=0
# while Count<len (s):
# Print (S[count])
# count+=1

#2 for Loop
For x in S:
print (x)

4. s= "Asdfer" into the loop for the for loop, but each time the content is printed "Asdfer"
s= "Asdfer"
For I in S:
print (s)

5. For loop to s= "ABCDEFG" into the loop, each time the content of the print is each character plus SB,
s= "ABCDEFG"

For I in S:
i=i+ "SB"
print (i)
6. For loop to s= "321" into the loop, print the content in order: "Countdown 3 Seconds", "Countdown
2 Seconds "," Countdown 1 Seconds "," Go! " "。

s= "321"
For I in S:
i= "Countdown {} seconds". Format (i)
print (i)
print ("departure")

" "
7, to implement an integer addition calculator (two numbers added):
For example: content = input ("Please lose content:")?: 5+9 or 5+ 9 or 5 + 9, then enter
? split and forward calculation.
" "
# # Content=input ("Please enter content:")
# content=input ("Please enter a number"). Strip ()
# S=content.split ("+")
# Print (int (s[0]) +int (s[1]))

" "
9, the calculation of the contents of the loss is an integer (in single digit units).
such as: content = input ("Please lose Content:") # such as Fhdal234slfh98769fjdla
" "
count=0
content=input ("Please enter content")
For i in content:
if I.isdigit () ==true:
count+=1
print (count)

count=0
content=input ("Please lose? Contents:")
For i in content:
if I.isalpha ():
count+=1
print (len (content)-count)

" "
10, write code, complete the following requirements:
in the case of a sustainable loss (? While loop), the user is allowed to:
lose? A, then show?? Road to go home, and then in the Let the door into the step to choose:
are you choosing a bus or a step?
Select Bus, show 10 minutes home, and exit the entire program.
Select step, show 20 minutes home, and exit the entire program.
lose? B, then show?? Road Home and quit the whole program.
lose? C, then show a detour home, and then in the Let the door into the step to choose:
would you prefer to play in a game hall or something?
Select the game Hall , then show '? ' When you get home, Daddy is at home, and you're waiting for a stick. ' And let it
lose again? A,b,c option.
choose, then Show ' two? When you get home, Mom's ready to fight? ' and make it heavy
new loser? A,b,c option.

" "
While 1:
choose=input ("" Please Choose the way home:
A. Take the Road Home
B. Take the path home
C. Take a detour home
your choice is: "). Upper ()

if choose== "A":
print ("Kobayashi walk you to the Road Home")
a=input ("Select 1. Bus 2. Walk:")
if a = = "1":
print ("10 minutes Home")
Break
elif a== "2":
print ("20 minutes Home")
Break
elif choose== "B":
print ("Take the Path Home")
Break
elif choose== "C":
print ("Kobayashi to accompany you to take a detour home, please choose a detour way")
c=input (" '
1. Game Arcade play for a while
2. Internet cafes play a while
your choice: ")
if c== "1":
print ("1.5 hours home, dad at home, holding a stick for you")
Continue
elif c== "2":
print ("Two hours home, mother at home, already ready")
Continue

11: Write code: Calculate 1-2 + 3 ... + 99 The sum of all the numbers except 88?

sum=0
count=1
While count<99:
if count%2==0:
if count==88:
count+=1
Continue
Sum=sum+count
count+=1
print (sum)

sum1=0
count1=1
While count1<=99:
if count1%2==1:
Sum1=sum1+count1
count1+=1
print (SUM1)
print (sum1-sum)


a = ([4,4,4],4,4)
i=[i for I in a]
i[0][0:3]=3,3,3
i[1]=3
i[2]=3
Print (tuple (i))

12. (upgrade) to judge whether the sentence is back? Back to: Is the right to read and to read the same kind of. For example, Shanghai
Come on, come on. Sea (upgrade problem)
A=len (content)
count=0
If Len (content)%2==0:
While count <= A//2:
count + = 1
if Content[count-1]==content[a-count]:
print ("This is a palindrome")
Else:
print ("This is not a palindrome")
elif len (content)%2==1:
While COUNT<A//2:
count+=1
if content[count-1] = = Content[a-count]:
print ("This is a palindrome")
Else:
print ("This is not a palindrome")


13. The loss?? A string that asks to be judged in this string?, write , write, number,
How many other characters are present and output
content=input ("Please enter content")
w=0
w=0
d=0
q=0
content=input ("Please enter content")
For i in content:
if I.isnumeric () ==true:
d+=1
elif I.islower () ==true:
w+=1
elif I.isupper () ==true:
w+=1
Else:
Q=len (content)-d-w-w
print ("Number of uppercase letters" +STR (w) + "\ t number of lowercase letters" +STR (w) + "\ t number of words" +str (d) + "\ t special symbol's word count" +str (q))

#14.
name=input ("Please enter first Name:")
place=input ("Please enter location:")
hobby=input ("Please enter hobby:")
print ("Dear and amiable {}, favorite in {} dry {}". Format (name,place,hobby))

Python learning [For 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.