1. 程式人生 > >三個猜數字遊戲代碼(Python)

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

randint res 玩家 [] 判斷 arc pre true clas

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)

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