2019-05-22

2019-05-22  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读