Example of if statement (I)

Why_ does_ it_ work 2021-11-25 15:31:44

Rock-paper-scissors

demand

1. Enter the punch you want from the console : stone (1), scissors (2) cloth (3)

2. The computer punches at random . Let's assume that the computer will make a stone , Complete the code function

3. Compare positive and negative

Problem solving

First, let's meet some input function , stay input The function is filled with strings , So we need to turn the string into an integer

player=int(input(" Enter the punch you want to punch : stone (1), scissors (2) cloth (3)"))
computer=1
print(" The fist players choose is %d- The fist of computer choice is %d"%(player,computer))

This operation is to record the player's fist and the computer's fist

Compare the outcome

player=int(input(" Enter the punch you want to punch : stone (1), scissors (2) cloth (3):"))
computer=1
print(" The fist players choose is %d- The fist of computer choice is %d"%(player,computer))
if (player==1 and computer==2)or(player==2 and computer==3)or(player==3 and computer==1):1
print(" The computer is weak ")

Here we just judged a situation , There are two other situations where we can pass if-else Judge , Because the draw conditions are simple , therefore , Here, we have to join with the above judgment , So you can use elif

player=int(input(" Enter the punch you want to punch : stone (1), scissors (2) cloth (3):"))
computer=1
print(" The fist players choose is %d- The fist of computer choice is %d"%(player,computer))
if (player==1 and computer==2)or(player==2 and computer==3)or(player==3 and computer==1):1
print(" The computer is weak ")
elif(player==computer):
print(" It ends in a draw ")
else:
print(" lost ")

Well, our last question is , The computer punches at random

We need to use a toolkit -----random

When we reference the toolkit, we need to use a function

import random

For the use of tool kits, you can enter a... After the tool kit name . Then press the tab build , The functions we need are listed , The function we need in this topic is random.randint

Usage method :random.randint[a b], This will output a To b All the formulas between

Join this string of code and play with stone, scissors and paper

Please bring the original link to reprint ,thank
Similar articles

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25