三個猜數字游戲代碼(Python)


def binary_search(list,item):
    low = 0
    high = len(list)-1

    while low <= high:
        mid = (low + high)//2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid - 1
        else:
            low = mid + 1
    return None

my_list = [1,3,5,7,9]

print(binary_search(my_list,3))
print(binary_search(my_list,-1))
#################猜年齡#################
import random
age = random.randint(1, 10)
for guess in range(1, 6):  # 設置次數
    choice = int(input())  # 輸入玩家猜測的年齡

if choice < age:  # 判讀玩家輸入的年齡是否等於正確的年齡
    print('小埋的提示:你猜小了(;´д`)ゞ。。。。')

elif choice > age:
    print('小埋的提示:乃猜大了惹(>﹏<)~~')

else:
    print('猜了' + str(guess) + '次,你就猜對惹~hiu(^_^A;)~~~')
    break

# 判斷猜測次數
if choice == age:
    print('搜噶~那么小埋下線了~拜拜~( ̄︶ ̄)↗')
else:
    print('哎呀~你還是木有猜對啊~但是你只有5次機會誒~怎么辦啊~')
    print('那好吧~心軟的小埋只好告訴你,我才' + str(age) + '')
############# 找出最大值和最小值 #############
def Min_Max(*arg):
    list = []
    flag = True
    while flag:
        item = input("請輸入數值,'ok'結束輸入:")
        if item == "ok":
            flag = False
        else:
            list.append(item)

    min_n = list[0]
    max_n = list[0]
    for item in list:
        if item < min_n:
            min_n = item
        if item > max_n:
            max_n = item
    return {"max": max_n, "min": min_n}

res = Min_Max()
print(res)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM