Data type conversion
Function format | describe | The sample |
---|---|---|
int(x) | Conversions include String and other numeric types | int(“1”) |
float(x) | You can convert strings to other numeric types, and fill in any missing digits with zeros. For example, 1 becomes 1.0 | The float (1) or float (” 1 “) |
str(x) | Convert numbers to strings | str(1) |
bool(x) | Anything that is not zero is true. Anything but zero is true | bool(0) |
statement
- Only when the conditions are met will it be executed. If the condition is not met, it will not be executed.
ifCondition: Something to doCopy the code
- Case 1
age = 20
if age > 18:
print("You can go to Internet cafes.")# Obviously this condition is met
Copy the code
Results of the above code:
- Input by keyboard
age = int(input("Please enter your age"))# Since input() is of type STR, it can be used as an int.
if age > 18:
print("You can go to Internet cafes.")
Copy the code
Results of the above code:
- Else statements are antithetical to if statements
age = int(input("Please enter your age"))# Since input() is of type STR, it can be used as an int.
if age > 18:
print("You can go to Internet cafes.")
else:
print("I can only do my homework at home.")
Copy the code
- Depending on the input value, different effects will be printed:
Elif statements and logical operators
- When xxx1 is satisfied, thing 1 is executed, and then the whole if ends
- When xxx1 is not satisfied, then judge xxx2, if xxx2 is satisfied, then do thing 2, and then the whole if ends
- If xxx1 is not satisfied, xxx2 is not satisfied, if xxx3 is satisfied, then thing 3 is executed, and the whole if ends
age = int(input("Please enter your age"))
if age > 1 and age <= 7:#and Both conditions need to be met
print("Childhood")
elif age > 7 and age <= 18:
print("Young")
elif age > 18 and age <= 25:
print("Youth")
else:
print("Beyond five.")
Copy the code
Results of the above code:
If the nested
- There’s a judgment statement inside a judgment statement
age = int(input("Please enter your age"))# convert the input string to an int
if age > 18:
sex = input("Please enter gender")
if sex == "Male":# check whether statements are nested
print("You're old enough to move bricks.")
elif sex == "Female":
print("You can dress up pretty.")
else:
print("The third creature.")
Copy the code
Results of the above code:
Mora game
- The computer randomly generates rock, paper, scissors for each game
- Users enter rock, scissors, and paper for each office
- Determine whether the user wins or the computer wins by judging the statement
"' 1-2-2 scissors stone 1 means" rock represents scissors 3 - cloth and cloth player wins: 1-2-2 3 3-1 ' ' '
import random # Make use of random modules
player = int(input("Please enter 1, rock 2, scissors 3, paper"))
pc = random.randint(1.3)# Select a random number between 1 and 3
if player < 4 and player > 0:# The following code belongs to if nesting
if (player == 1 and pc == 2) or (player == 2 and pc ==3) or (player == 3 and pc == 1):
print("The player wins")
elif player == pc:
print("Draw")
else:
print("Computer wins")
else:
print("Invalid input")
Copy the code
Results of the above code: