簡體   English   中英

TypeError: cannot unpack non-iterable NoneType object: 找不到我的數據變成“無類型”的位置

[英]TypeError: cannot unpack non-iterable NoneType object: Cannot find where my data turned into “None Type”

開始學代碼,開始學 python 我想問一下我在jetbrains academy練的東西

當我像下面這樣編碼時,我得到TypeError: cannot unpack non-iterable NoneType object

我知道有幾個關於這個 TypeError 的問題,但我無法理解評論,我不知道出了什么問題......你們中的一些人介意看看這個並告訴我為什么我得到錯誤...? 有不妥之處多多評論

# declaring the current status of the coffee machine
def status(water, milk, beans, cups, money):
    print('The Coffee machine has:')
    print(water, 'of water')
    print(milk, 'of milk')
    print(beans, 'of coffee beans')
    print(cups, 'of disposable cups')
    print(money, 'of money')


# espresso
def espresso(water, beans, money):
    water -= 250
    beans -= 16
    money += 4
    return water, beans, money

# latte
def latte(water, milk, beans, money):
    water -= 350
    milk -= 75
    beans -= 20
    money += 7
    return water, milk, beans, money

# cappuccino
def cappuccino(water, milk, beans, money):
    water -= 200
    milk -= 100
    beans -= 12
    money += 6
    return water, milk, beans, money

# buy
def buying(water, milk, beans, money):
    coffee = input('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino: ')
    if coffee == 1:
        water, beans, money = espresso(water,beans, money)
        return water, milk, beans, money
    elif coffee == 2:
        water, milk, beans, money = latte(water, milk, beans, money)
        return water, milk, beans, money
    elif coffee == 3:
        water, milk, beans, money = cappuccino(water, milk, beans, money)
        return water, milk, beans, money

status(water, milk, beans, cups, money)
water, milk, beans, money = buying(water, milk, beans, money)
status(water, milk, beans, cups, money)

您需要將用戶輸入轉換為 int。

coffee = int(input('你想買什么?1 - 濃縮咖啡,2 - 拿鐵,3 - 卡布奇諾:'))

當您檢查 integer 時, input() function 返回一個字符串。

做這個 -

# buy
def buying(water, milk, beans, money):
    coffee = input('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino: ')
    # Convert string to integer
    # keep in mind it will throw ValueError if input was not an integer
    coffee = int(coffee)

您也可以將 if 語句更改為

# Notice the quotes
if coffee == '1':

此外,最好在 function 的末尾返回。 這樣,即使輸入不正確,您也不會收到TypeError (例如,當用戶輸入 4 時)

def buying(water, milk, beans, money):
    coffee = input('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino: ')
    
    if coffee == '1':
        water, beans, money = espresso(water,beans, money)
    elif coffee == '2':
        water, milk, beans, money = latte(water, milk, beans, money)
    elif coffee == '3':
        water, milk, beans, money = cappuccino(water, milk, beans, money)

    return water, milk, beans, money

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM