Python入门(二)用Python设计的第一个小程序

二、用python设计的第一个小程序

1.猜数字游戏(第一版)

#猜数字游戏(第二版)
 
import random
secret = random.randint (1,100)
counts = 3
print("--------GUESS NUMBER GAME--------")
 
while counts > 0:
    temp = input("You can guess one number: ")
    guess = int(temp)
 
    if guess == secret:
        print("Yes! You are smart!!")
        break
    else:
        if guess > secret:
            print("The number is bigger!!")
        else:
            print("The number is smaller!!")
    counts = counts - 1
    
print("This game is over!!")

2.猜数字游戏(第二版)

改进建议:

  1. 用户猜错时,程序应该给出提示。
  2. 应该给用户提供多次机会(固定次数3次)。
  3. 每次运行程序时,答案应该是随机的。 
#猜数字游戏(第二版)
 
import random
secret = random.randint (1,100)
counts = 3
print("--------GUESS NUMBER GAME--------")
 
while counts > 0:
    temp = input("You can guess one number: ")
    guess = int(temp)
 
    if guess == secret:
        print("Yes! You are smart!!")
        break
    else:
        if guess > secret:
            print("The number is bigger!!")
        else:
            print("The number is smaller!!")
    counts = counts - 1
    
print("This game is over!!")

(创作不易,欢迎您的意见和建议,感谢支持♥♥♥)

猜你喜欢

转载自blog.csdn.net/weixin_42067873/article/details/111957198