[英]How to add numbers in a string
我是 Python 新手,我有一個作業來驗證信用卡號。 我完成了前兩個條件,但我堅持 #3 和 #4 條件。 任何幫助表示贊賞
狀況:
def verify(number) : # do not change this line!
# write your code here so that it verifies the card number
#condtion 1
if number[0] != '4':
return "violates rule #1"
#condition 2
if int(number[3]) != (int(number[5]) + 1) :
return "violates rule #2"
#condition 3
for i in number:
if i >= '0' and i !='-':
# be sure to indent your code!
return True # modify this line as needed
input = "4037-6000-0000" # change this as you test your function
output = verify(input) # invoke the method using a test input
print(output) # prints the output of the function
# do not remove this line!
預期產出:
● "5000-0000-0000": violates rule #1
● "4000-0000-0000": passes rule #1, violates rule #2
● "4007-6000-0000": passes rules #1-2, violates rule #3
● "4037-6000-0000": passes rules #1-3, violates rule #4
● “4094-3460-2754”: passes all rules
對於規則 3,您需要對所有數字求和並檢查除以 4 的余數是否為零:
#condition 3
s = 0
for i in number:
if i != '-':
s += int(i)
if s % 4 != 0:
return "violates rule #3"
對於規則 4,您可以獲得子字符串的 int 總和:
if (int(number[0:2]) + int(number[7:8])) != 100:
return "violates rule #4"
完整代碼:
def verify(number) : # do not change this line!
# write your code here so that it verifies the card number
#condtion 1
if number[0] != '4':
return "violates rule #1"
#condition 2
if int(number[3]) != (int(number[5]) + 1) :
return "violates rule #2"
#condition 3
s = 0
for i in number:
if i != '-':
s += int(i)
if s % 4 != 0:
return "violates rule #3"
if (int(number[0:2]) + int(number[7:9])) != 100:
return "violates rule #4"
# be sure to indent your code!
return True # modify this line as needed
input = "4037-6000-0000" # change this as you test your function
output = verify(input) # invoke the method using a test input
print(output) # prints the output of the function
# do not remove this line!
對於條件 #3
Python 中的字符串是可迭代的,這意味着您可以在for
循環中傳遞它們。 循環中的每個元素都是字符串中的一個字符。 所以如果你做了
for char in "4094-3460-2754":
print(char)
你會得到:
4
0
9
4
-
3
4
etc.
使用它,您可以計算輸入中每個數字的總和並檢查它是否可以被 4 整除。需要注意的兩件事,您需要首先將字符轉換為整數(使用int
)。 你不能做
"4" + "0"
但是你可以做
int("4") + int("0")
您還需要使用if
從總和中排除“-”。
其次,我們使用模 ( %
) 檢查兩個數字在 Python 中是否可整除。 結果是余數,如果余數為 0,則第一個參數可被第二個參數整除。
16 % 4 == 0 # divisible by 4
21 % 4 == 1 # not divisible by 4
對於條件 #4
除了可迭代之外,Python 中的字符串還可以通過索引訪問(從 0 開始)
"4094-3460-2754"[0] == "4"
"4094-3460-2754"[1] == "0"
"4094-3460-2754"[2] == "9"
"4094-3460-2754"[0:1] == "40"
"4094-3460-2754"[1:2] == "09"
因此,您可以訪問多個字符並將它們視為整數:
int("4094-3460-2754"[0:1]) == 40
現在您可以將它們加在一起,看看它們是否等於 100。
所有數字之和必須能被 4 整除。
您可以使用條件語句進行檢查,例如:
if sum_of_nums % 4 != 0:
print("Violates rule 3!")
這會檢查除以 4 時是否有余數,如果沒有余數,它將被平均除,並且表達式將等於 0。 如果它不均分,它將不等於 0!
如果將前兩位數視為兩位數,將第七位和第八位視為兩位數,則它們的總和必須為 100。
在這里,您可以像引用列表一樣引用字符串的字符。 如果輸入始終保持一致,您可以對引用進行硬編碼,將它們更改為整數,然后將它們加在一起並使用條件語句檢查它們
first_num = input[0]+input[1]
first_num = int(first_num) #now an int
second_num = input[6]+input[7]
second_num = int(second_num)
if first_num + second_num != 100:
print("Violates rule 4!")
我寧願去掉破折號-
從數量第一,這樣它可以很容易地工作着。 您也可以在不刪除它的情況下進行操作。
# split it into parts separated by dashes
# consider 4094-3460-2754
no_dashes = number.split('-')
print(no_dashes) # ['4094', '3460', '2754']
# combine the numbers without dashes
no_dashes = ''.join(no_dashes)
print(no_dashes) # 409434602754
# convert it into a list of integers so that it is more easier to work with
number = [int(x) for x in no_dashes]
print(number) # [4, 0, 9, 4, 3, 4, 6, 0, 2, 7, 5, 4]
現在,正如您所提到的,第一個條件很簡單,您可以簡單地檢查第一個數字是否為 4。
# 1st condition
if number[0] != 4:
return 'Violates #1'
第二個條件也很簡單:
# 2nd condition
# 4th digit is a[3] and 5th digit is a[4]
if number[3] != number[4] + 1:
return 'Viloates #2'
對於第三個條件,您只需找到數字中每個數字的總和。 由於我們已經將數字轉換為整數數組,因此使用sum()
函數也很容易:
# 3rd condition
# Find the sum
num_sum = sum(number)
print(num_sum) # 48
# now check if the sum is divisible by 4
if num_sum % 4 != 0:
return 'Violates #3'
現在,對於第四個條件,您需要將第 1 位和第 2 位數字視為兩位數,並與第 7 位和第 8 位數字相同。 您可以將其轉換為兩位數,如下所示:
# 1st digit is number[0]
# 2nd digit is number[1]
# 7th digit is number[6]
# 8ty digit is number [7]
# convert 1st two digits into a two-digit number
x = number[0] * 10 + number[1]
# convert 7th and 8th digits into a two-digit number
y = number[6] * 10 + number[7]
現在您可以檢查它們的總和是否為 100:
if x + y != 100:
return 'Violates #4'
因此,組合程序變為(合並了一些步驟):
def verify(number):
number = [int(x) for x in ''.join(number.split('-'))]
if number[0] != 4:
return 'Violates #1'
if number[3] != number[4] + 1:
return 'Viloates #2'
if sum(number) % 4 != 0:
return 'Violates #3'
if (number[0] * 10 + number[1] + number[6] * 10 + number[7]) != 100:
return 'Violates #4'
return True
但是上面的程序只會給出失敗的第一個條件。 如果需要,您可以進一步修改它,如下所示:
def verify(number):
failed = []
number = [int(x) for x in ''.join(number.split('-'))]
if number[0] != 4:
failed += [1]
if number[3] != number[4] + 1:
failed += [2]
if sum(number) % 4 != 0:
failed += [3]
if (number[0] * 10 + number[1] + number[6] * 10 + number[7]) != 100:
failed += [4]
res = 'Violates ' + (', '.join[str(x) for x in failed])
return res if len(failed) != 0 else 'Passed'
print(verify('4094-3460-2754')) # Passed
print(verify('4037-6000-0000')) # Violates 4
您也可以再次修改它以顯示通過的條件。 我把它留給你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.