Python 猜数小程序(练习)

------Game1
def guess(armnums):
    number = random.randint(1, 9) 
    chances = 0  
    while chances < 5: 
        print("System guess :%d"%number)
        if armnums == number:  
            print("System   Get it !!!")
            break
        elif armnums < number: 
            print("System guess was too high  !") 
            number=random.randint(1, number) 
        else:  
            print("System guess was too low !") 
            number=random.randint(number, 9) 
        chances += 1 
    if not chances < 5: 
        print("System LOSE  YOU WIN !!!")
 
if __name__ == __main__: 
    sysflag=True 
    while sysflag:
        arm=input("Please input a arm number (between 1 and 9):")
        guess(int(arm))
        flag=input("Continue Y/N ?:")        
        if flag.upper()==N:
            print(Game over!)
            sysflag=False

------Game2
import random 
  

def guess(armnums):
    number = random.randint(1, 9) 
    chances = 0  
    while chances < 5: 
        print("System guess :%d"%number)
        if armnums == number:  
            print("System   Get it !!!")
            break
        elif armnums < number: 
            print("System guess was too high  !") 
            number=random.randint(armnums, number) 
        else:  
            print("System guess was too low !") 
            number=random.randint(number, armnums) 
        chances += 1   
 
if __name__ == __main__: 
    sysflag=True 
    while sysflag:
        arm=input("Please input a arm number (between 1 and 9):")
        guess(int(arm))
        flag=input("Continue Y/N ?:")        
        if flag.upper()==N:
            print(Game over!)
            sysflag=False


------Game3
import random 
  
def guess(armnums):
    list =[]
    number = random.randint(1, 1000)  
    fmax = 1000
    fmin = 1
    while True: 
        print("System guess :%d"%number)
        if armnums == number:  
            print("System   Get it !!!")
            break
        elif armnums < number: 
            print("System guess was too high  !") 
            fmax=number 
        else:  
            fmin=number
            print("System guess was too low !") 
        list.append(number) 
        number=random.randint(fmin,fmax) 
        if number in list:
            number=random.randint(fmin,fmax) 
 
if __name__ == __main__: 
    sysflag=True 
    while sysflag:
        arm=input("Please input a arm number (between 1 and 1000):")
        guess(int(arm))
        flag=input("Continue Y/N ?:")        
        if flag.upper()==N:
            print(Game over!)
            sysflag=False

 

Python 猜数小程序(练习)

上一篇:微信小程序自定义导航栏组件


下一篇:用vscode开发微信小程序,建议安装的插件