第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

驗證字符串python中的第一個數(shù)字

驗證字符串python中的第一個數(shù)字

慕桂英546537 2021-08-11 21:26:27
我是 python 的新手,已經(jīng)在這個網(wǎng)站上搜索過,但仍然無法弄清楚。這是一個家庭作業(yè),所以不是在尋找答案,但我無法弄清楚我做錯了什么并出現(xiàn)語法錯誤(我堅持第一條規(guī)則......)賦值:我們假設(shè)信用卡號是一個由 14 個字符組成的字符串,格式為####-####-####,包括破折號,其中“#”代表一個0-9之間的數(shù)字,所以總共有12位數(shù)字。1.第一位數(shù)字必須是4。 2.第四位數(shù)字必須比第五位數(shù)字大一個;請記住,由于格式為####-####-####,因此它們由破折號分隔。3. 所有數(shù)字的和必須能被 4 整除。 4. 如果將前兩位數(shù)字視為兩位數(shù),將第七位和第八位數(shù)字視為兩位數(shù),則它們的和必須為 100。到目前為止,這是我的代碼。我讀過您不能將字符與數(shù)字進行比較,但我嘗試過的任何方法都沒有奏效。任何幫助/指導(dǎo)將不勝感激!def verify(number) : if input ['0'] == '4'  return Trueif input ['0'] != '4'  return "violates rule #1"input = "4000-0000-0000" # change this as you test your functionoutput = verify(input) # invoke the method using a test inputprint(output) # prints the output of the function
查看完整描述

3 回答

?
拉莫斯之舞

TA貢獻1820條經(jīng)驗 獲得超10個贊

您的代碼不正確:


def verify(number): 

# incorrect indent here

if input ['0'] == '4' # missing : and undeclared input variable, index should be int

   return True

if input ['0'] != '4'   # missing : and undeclared input variable, index should be int

  return "violates rule #1"

固定代碼:


def verify(number):

    if number[0] != '4'

        return "violates rule #1"

    # other checks here

    return True

另外我建議False從這個函數(shù)返回而不是錯誤字符串。如果您想返回有錯誤的字符串,請考慮使用 tuple like(is_successful, error)或自定義對象。


查看完整回答
反對 回復(fù) 2021-08-11
?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

看看字符串索引:


字符串可以被索引(下標),第一個字符的索引為 0。沒有單獨的字符類型;一個字符只是一個大小為 1 的字符串:


>>> word = 'Python'

>>> word[0]  # character in position 0 'P'

>>> word[5]  # character in position 5 'n'

然后閱讀if 語句- 您的代碼缺少:,第二個if可以用else子句替換。


您可能還想檢查函數(shù)的參數(shù),而不是全局變量input(這是一個壞名稱,因為它隱藏了input()內(nèi)置變量)


建議修復(fù):


def verify(number) : 

    if number[0] == '4':

        return True

    else:

        return "violates rule #1"


testinput = "4000-0000-0000" # change this as you test your function

output = verify(testinput) # invoke the method using a test input

print(output) # prints the output of the function


查看完整回答
反對 回復(fù) 2021-08-11
?
九州編程

TA貢獻1785條經(jīng)驗 獲得超4個贊

你的代碼很好,但有幾個問題


def verify(number) : 


   if input [0] == '4':

     return True

   if input [0] != '4':

     return "violates rule #1"


input = "4000-0000-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

首先,縮進在python中很重要,屬于函數(shù)定義的所有內(nèi)容都應(yīng)該縮進。


其次,if 語句后面應(yīng)該跟:. 就這些


查看完整回答
反對 回復(fù) 2021-08-11
  • 3 回答
  • 0 關(guān)注
  • 375 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號