用python来编写guess game!

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qjt19950610/article/details/82939002

这里我总结了三个方法,供博友参考!

# first method
'''age_of_oldboy = 56
count =int(input("guess count:"))        #设置猜的次数
while count>0:
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy:
        print("yes,you got it.")
        break
    elif guess_age < age_of_oldboy:
        print("think smaller")
    else:
        print("think bigger")
    count -=1 '''
# second method
age_of_oldboy = 56
count=0
#while true:
while count<3:
    # if count == 3:
    #     break
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy:
        print("yes,you got it.")
        break
    elif guess_age < age_of_oldboy:
        print("think smaller")
    else:
        print("think bigger")
    count +=1
else:                  #如果while成立,执行以下代码。否则,执行else后代码
    print("you have tried too many times,fuck off!")

升级版(也是第三种方法)

# author:Iron Qi
age_of_oldboy = 56
count=0
while count<3:
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy:
        print("yes,you got it.")
        break
    elif guess_age < age_of_oldboy:
        print("think smaller")
    else:
        print("think bigger")
    count +=1
    if count == 3:
        countine_confirm = input("do you want to keep guessing?")
        if countine_confirm !='n':        #设置停止指令
            count = 0

猜你喜欢

转载自blog.csdn.net/qjt19950610/article/details/82939002