2016-01-28 149 views
-1

對於這個任務,我必須採取一個七位數字輸入的被叫帳號和使用的最後三位數字返回一個磁盤存儲位置選擇最後三個數字,我的代碼看起來像這樣至今:從用戶輸入

Account_num = int(input("Enter account number: "))      
Disk = Account_num[-3:] 
if Account_num <= 99999999: 
    print("Your disk storage location is:",Disk 
     ) 
else: 
    print("Invalid account number entred") 

它還應該要求用戶輸入另一個帳戶代碼,如果磁盤存儲位置已滿,請回復一條錯誤消息。 它應該返回是:

"Your disk storage location is" (three digit number) 
"Enter another account number: " 

,而是它返回:

Disk = Account_num[-3:] TypeError: 'type' object is not subscriptable 

我知道編碼所以任何幫助,將不勝感激算不上什麼。

+0

應該說'類型錯誤: '詮釋' 對象不subscriptable'因爲你不能做'[-3:]'在一個int。 –

回答

1

Account_numint類型,[]您試圖使用的切片符號支持序列(即包含其他對象的對象)。

爲了從衆多獲得最後數字可以使用%操作員產生了從餘數,與1000

Disk = Account_num % 1000 

因此,對於給定的Accound_num = 9230939

Disk = Account_num % 1000 
Print(Disk) # prints 939 
0

您可以稍後投入您的輸入

>>> Account_num = input("Enter account number: ") 
Enter account number: 12345 
>>> Disk = int(Account_num[-3:]) 
>>> Disk 
345 

如果你這樣做,不過,你還需要轉換ACCOUNT_NUM要對比

if int(Account_num) <= 99999999: 
0

您輸入

Account_num = int(input("Enter account number: ")) 

但是,試圖處理它作爲一個字符串。這會工作

Account = input("Enter account number: ") # string     
Account_num = int(Account) # Now an int  
Disk = int(Account[-3:]) # Make this an int also for use later 
if Account_num <= 99999999: 
    print("Your disk storage location is:", Disk) 
else: 
    print("Invalid account number entred") 
0

不能使用[-3:]上一個int,但你可以在一個字符串,所以只得到輸入,然後轉換爲字符串搶最後三個字符,然後投到一個int。

Account_num = input("Enter account number: ") 
Disk = int(str(Account_num)[-3:]) 
if Account_num <= 99999999: 
    print("Your disk storage location is:",Disk 
     ) 
else: 
    print("Invalid account number entred")