Use python to make a simple guessing game

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


Preface

Write a simple guessing game in python


1. Introduction

This is the first program I wrote in python. It has some limitations. I hope you can criticize and correct it.

2. Code

Method one code is as follows:

you = int(input("请输入剪刀0,石头1,布2:"))
import random
cp = random.randint(0,2)##生成范围是0到2的随机数

if(you > 2 or you < 0):
    print("没有这种出法哦")##玩家不能输入0到2范围以外的数字
else:
 print("电脑出的是 %d\n " % cp)
 if(you == 0):
    if(cp ==  0):
        print("平局,请再出一次")
    elif(cp ==  1):
        print("你输了")
    else:
        print("你赢了")
 if(you == 1):
    if(cp == 1):
        print("平局,请再出一次")
    elif(cp == 2):
        print("你输了")
    else:
        print("你赢了")
 if(you == 2):
    if(cp == 2):
        print("平局,请再出一次")
    elif(cp == 0):
        print("你输了")
    else:
        print("你赢了")



 



Summary one

Advantages: relatively concise, clear thinking.
Disadvantages: not intuitive enough to directly explain what the computer is. You can only use numbers instead of
draws. You must rerun the game to do it again.

<font color=#999AAA The improved code is as follows:

you = int(input("请输入剪刀0,石头1,布2:"))
import random##引用随机库函数
cp = random.randint(0, 2)  ##生成范围是0到2的随机数
if(you > 2 or you < 0):
    print("没有这种出法哦")##玩家不能输入0到2范围以外的数字
else:
    cpc = 0
    if(cp == 0):
        cpc = "剪刀"
    elif(cp == 1):
        cpc = "石头"
    else:
        cpc = "布"
    print("电脑出的是%s\n " % cpc)
    if(you == 0):
        if(cp ==  0):
            print("平局,请再出一次")
        elif(cp ==  1):
            print("你输了")
        else:
             print("你赢了")
    if(you == 1):
        if(cp == 1):
            print("平局,请再出一次")
        elif(cp == 2):
            print("你输了")
        else:
            print("你赢了")
    if(you == 2):
        if(cp == 2):
            print("平局,请再出一次")
        elif(cp == 0):
             print("你输了")
        else:
             print("你赢了")

Summary two

After the improvement, it is more intuitive to convert the numbers from the computer into Chinese characters. Pay special attention to the order of if statements during programming.
The if statement that converts the computer-generated numbers into Chinese characters and the if statement for judging win or loss are parallel. If they are not juxtaposed, the if statement for judging win or loss is put into the if statement that converts the computer-generated number into Chinese characters. The program execution stops directly at the conversion point.
The error code is as follows:

   you = int(input("请输入剪刀0,石头1,布2:"))
import random##引用随机库函数
cp = random.randint(0, 2)  ##生成范围是0到2的随机数
cpc = 0
if(cp == 0):
    cpc = "剪刀"
elif(cp == 1):
    cpc = "石头"
else:
    cpc = "布"
    
    if(you > 2 or you < 0):
        print("没有这种出法哦")##玩家不能输入0到2范围以外的数字
    else:
        print("电脑出的是%s\n " % cpc)
    if(you == 0):
        if(cp ==  0):
            print("平局,请再出一次")
        elif(cp ==  1):
            print("你输了")
        else:
             print("你赢了")
    if(you == 1):
        if(cp == 1):
            print("平局,请再出一次")
        elif(cp == 2):
            print("你输了")
        else:
            print("你赢了")
    if(you == 2):
        if(cp == 2):
            print("平局,请再出一次")
        elif(cp == 0):
             print("你输了")
        else:
             print("你赢了")

In this case, the program ends directly after performing the Chinese conversion operation, which does not achieve the purpose we want.

note

Pay special attention to indentation in python. The position of indentation is equivalent to braces, which can determine whether the relationship between the codes is parallel or included.

Guess you like

Origin blog.csdn.net/weixin_45761932/article/details/108611766