关于小程序:记-猜数字-程序

66次阅读

共计 1676 个字符,预计需要花费 5 分钟才能阅读完成。

“””
coding a game
guess what number is?
“””
from random import randint

welcome=”Welcome to the program’Guess number what’!”
welcome+=”nNow let us playing!”
r=randint(1,10**3)
trace=0

while trace<=4: # limit is 5

message=input('input a integer in range of 1 to 1000:') 
trace+=1 # recording times of try
if message.lower()=='boss':
    print('Sir. the number is:'+str(r))
    message=input('please input a number here, Sir.')
# control user-inputting
if message.isdigit()==False: # determine the character
    print('Please input a integer.')
    continue
elif int(message)>1000:    
    print('Please input a number between 1 and 1000.')
    continue

message=int(message)
if message==r: # if guess success
    print(str(r)+"!")
    print("Congratulations! You are succeed!")
    choice=input("Do you want to play again(Yes/No)")
    if choice.lower()=='yes':
            trace=0
            r=randint(1,10**3)
            continue
    else:
        print('Thanking for playing.')
        break
else: # if guess wrong
    if trace == 5:
        print('You are fail, Dearly.\nThe game over.')
    else:
        if message<r:
            if r-message<50:
                print('Try to add a number less than 50')
            elif r-message<100:    
                print('Try to add a number between 100-50.')
            elif r-message<200:
                print('Try to add a number between 200-100')
            elif r-message<300:
                print('Try to add a number between 300-200')
            elif r-message<400:
                print('Try to add a number between 400-300')
            elif r-message<500:
                print('Try to add a number between 500-400')
            else:
                print(
                    'the difference between the two number is greater'+ 
                    'than 500.'
                    )
        else:
            if message-r<50:
                print('Try to minus a number less than 50')
            elif message-r<100:    
                print('Try to minus a number between 100-50.')
            elif message-r<200:
                print('Try to minus a number between 200-100')
            elif message-r<300:
                print('Try to minus a number between 300-200')
            elif message-r<400:
                print('Try to minus a number between 400-300')
            elif message-r<500:
                print('Try to minus a number between 500-400')
            else:
                print(
                    'the difference between the two number is greater'+ 
                    'than 500.'
                    ) 

正文完
 0