Analysis of the concept While Loop
Import Random
Secret=int (Random.uniform (0,10))
print ("I think the number is between 0 and 10, guess.") guess=11 while
guess!=secret:
guess=int input ("Enter the number of guesses.") )
print ("OK done!")
Results:
I think the number is between 0 and 10, guess.
Enter the number you guessed. 3
Type the number of guesses. 2
Type the number of guesses. 1
Type the number of guesses. 4
Type the number of guesses. 5
Type the number of guesses. 6
Type the number of guesses. 7
Type the number of guesses. 8
Type the number of guesses. 9
Type the number of guesses.
Enter the number of guesses. 0.
done!
For Loop
The following examples are legal:
>>> for i in [1,2,3,4,5,6,7,8,9,10,11,12]:
print (I, "Times are", i*12) 1 is the
2 times 12 is 3 The 4 times is a 5 times A is a
6
times A is a 8 is the 9 times the 108 is 132 the same times The Times are
144
>>> for I In (1,2,3,4,5,6):
print (i, "The Times are", i*12)
1 is a
3 time S is 4 the 5 times are 6 times are
72
>>> for I in {1,2,3,4,5,6}:
print (I, "Times are", i*12) 1 is a
2 is
3 Tim ES 4 is 5 The times are 6 times are
72
For i in ' 123456789 ':
print (I, "Times are", i*12)
1 are 111111111111 2 times are
222222222222
3 the 333333333333 4 is
444444444444 the
5 is 555555555555 6 # is
666666666 666
7
is 777777777777 8
are 888888888888 9
is 999999999999 #上面这种方法虽然不报错 but does not have To the results we want, it shows what happens when you use error data.
# Note:
# {} represents a collection in Python #
[] represents a list in Python
() represents a tuple in Python
In particular, the processing of a dictionary of this type of data is not quite the same as other types of data
>>> real_name={"Benny": "Benjamin Everard", "Alex": "Alexander Bradbury"}
>>> for Key,value in Real_name.items ():
print ("The real name of" + key+ "are" +value) the real
name of Bennyisbenjamin Everard the
re Al name of Alexisalexander Bradbury
#for循环的查询方式类似于SQL语句
It is important to note that when using a collection, you cannot control the order in which data is accessed; In general, if you use a for loop, you need to use a list or tuple instead of a collection or a dictionary. Deep Understanding Find Prime
Nested calls that contain loops
>>> for I in Range (1,100,2): Is_prime=true for K in range (2,i): if (i%k==0): Print (I, "
is divisible by ", K" Is_prime=false break if Is_prime:print (i, "are prime") 1 is prime
3 is prime 5 was prime 7 is prime 9 are divisible by 3-a prime is prime-is divisible by 3/is prime The is divisible with 3 is divisible prime to divisible by 3 is prime to prime by 3 3 5 is divisible by 5 (prime) divisible by 3 The is prime the prime-is-divisible by 3-is prime ble by 7 "divisible by 3" is prime to divisible by 5 of is divisible by 3 ' is prime ' is prime ' is DIVISIBL E by 3 "divisible by 5" Prime is divisible-by-3 is prime-is-prime-are divisible by 3 By 7-is prime-divisible-by-3-prime-is-is-divisible by 5-is-divisible by 3-is-prime-is-divisible by
7 divisible by 3Divisible by 5 The prime is divisible by 3
If statement
#一个if语句最多执行一段代码 (multiple elif statements in parallel), such as the following example, the program only judges the relationship between 10 and 2, and will not judge the relationship between 10 and 5.
Num=int (Input ("Enter a number:")
if num%2==0:
print ("Your number is divisible by 2")
elif num%3==0:< C5/>print (' Your number is divisible by 3 ')
elif num%5==0:
print ("Your number is divisible by 5")
else:
PR Int ("Your number isn't divisible by 2,3 or 5")
Results:
Enter a number:10
Your number is divisible by 2
Catch Exception
Principle: Python Returns the type of error after executing a program error, which is used to achieve the wrong line.
Is_number=false
num=0 While not
is_number:
is_number=true
try:
num=int (Input ("Enter a number") )
except ValueError:
print ("I said a number!")
Is_number=false
if num%2==0:
print ("Your number is divisible by 2")
elif num%3==0:
print ("Your Number is divisible by 3 ")
elif num%5==0:
print (" Your number are divisible by 5 ")
Else:
print (" Your nu Mber is isn't divisible by 2,3 or 5 ")
Results:
Enter a number dash
I said a number!
Enter a Number3
Your number is divisible by 3
Practice: Modify the start of the Guess number game
Import Random
Secret=int (Random.uniform (0,10))
print ("I think the number is between 0 and 10, guess.") ")
guess=11 while
Guess!=secret:
try:
guess=int (Input (" Take a guess! ")
Except ValueError:
print ("I said a number!")
Print ("OK done!")
Results:
I think the numbers are between 0 and 10, guess what.
Take a guess!d I said a number! Take a guess!1 Take a guess!2 Take a guess!3 very done!