美文网首页
2019-05-22

2019-05-22

作者: 木马音响积木 | 来源:发表于2019-05-22 22:41 被阅读0次

python 猜数游戏

import random

def givemessage(guess,magic):
    if (guess>magic):
        print("error,too big!")
    elif (guess<magic):
        print("error,too small!")
    else:
        print("oh,oh you get it!")
        #break

#givemessage(8,9)

bool2=True  # if you want do it again

while bool2:
    magic = random.randint(1,100)
    counter=0  #record how many times you guess
    while counter<10 :
        temp = input("Enter an integer from 1 to 100,数字是:")
        temp2 = temp.strip()
        if temp2.isdigit():
            guess = int(temp2)
        else:
            continue
        counter+=1
        givemessage(guess,magic)
        if guess==magic:
            #print("you try {} times".format(counter))
            #counter=100
            break
            #pass
        #print(magic)
    print("you try {} times".format(counter))   
    temp3 = input("do it again?y=yes,n=no:")
    if (temp3 != "y"):
        bool2=False

猜数字的游戏.jpg

相关文章

网友评论

      本文标题:2019-05-22

      本文链接:https://www.haomeiwen.com/subject/ugnpzqtx.html