Lecture 2.2 [Day04 Python study notes improvements in the game]

1. modify the program requirements:

1.1 tells the user the number is big or small r
1.2 input errors should continue to return to the initial guess, guessed right up until
1.3 The answer is random

2. Reserve knowledge

2.1 conditional branching statements (comparative size <, <=,>,> =, ==,! =)
2.2 Loops

1) while loop: "the while conditions:"
# condition is true, the operation execution cycle

2.3 and logical operators
The introduction of foreign aid import 2.4

#random module, generating a random number
# inside the randint () function
#import random

2.5 limit the number of
2.6 print () function

Print is the default print finished adding a line break, so I want a continuous input can be used in the back end = "" Tell print (), instead of line breaks with spaces

3. The program code

The following is a small program code after the modification:

import random
secret = random.randint(1,200)
print("第四课\n改进小游戏规则")
time = 5
guess = 0

print("--------我是weivid_wang----------")
#temp = input("猜一下weivid_wang心中想的是哪个数(1——200):")
print("猜一下weivid_wang心中想的是那个数(1——200): ",end=" ")


if guess == secret:
        print("卧槽你是我心中的蛔虫吗\n哼猜中了也没有奖励!!")
        
else:
    while guess != secret and time > 0:
        temp = input()
        guess = int(temp)
        time = time - 1
        if guess == secret:
            print("卧槽你是我心中的蛔虫吗\n哼猜中了也没有奖励!!")
        else:
            if guess > secret:
                print("你猜的数字大了!")       
            else:
                print("你猜的数字小了")
        if time > 0:
            print("再试一次吧:",end=" ")
        else:
            print("机会用光了")
                

print("游戏结束,不玩啦!")

Operating results, we can easily enter a number 100, according to prompts to enter numbers, the number reached five still did not guess right, then exit the program:
Here Insert Picture Description

Published 105 original articles · won praise 71 · views 40000 +

Guess you like

Origin blog.csdn.net/vivid117/article/details/104283747