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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

生成隨機(jī)字符串,直到生成給定的字符串

生成隨機(jī)字符串,直到生成給定的字符串

慕村225694 2023-04-25 17:03:36
這是一個(gè)生成一些隨機(jī)字符串直到生成給定字符串的程序。我無(wú)法理解修復(fù)部分中的索引的想法else。如果隨機(jī)生成的字符與attemptThis[i]中的字符不匹配t[i],我們是否將字符存儲(chǔ)在右側(cè)attemptNext?之后當(dāng)它再次檢查時(shí),只有一個(gè)字符存儲(chǔ)在attemptThis?我不知道我問(wèn)的方式是否正確。我在部分中得到了陳述的想法if。但是令人else:attemptNext += t[i]困惑。帶有示例的 exp 將不勝感激。(來(lái)自 gfg 的代碼)import string import random    possibleCharacters = string.ascii_lowercase + string.digits +                      string.ascii_uppercase + ' ., !?;:'  t = "geek"  attemptThis = ''.join(random.choice(possibleCharacters)                                 for i in range(len(t))) attemptNext = ''   completed = Falseiteration = 0  while completed == False:     print(attemptThis)           attemptNext = ''     completed = True          for i in range(len(t)):         if attemptThis[i] != t[i]:             completed = False            attemptNext += random.choice(possibleCharacters)         else:             attemptNext += t[i]                   iteration += 1    attemptThis = attemptNext   print("Target matched after " +      str(iteration) + " iterations") 
查看完整描述

3 回答

?
三國(guó)紛爭(zhēng)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

for循環(huán)正在為 構(gòu)建一個(gè)字符串attemptNext。如果 中的字符attemptThis等于 中的相應(yīng)字符t,則將該字符添加到 的末尾attemptNext。

代碼寫得很奇怪。在代碼中,最好避免在語(yǔ)句的簡(jiǎn)單表達(dá)式中使用notor 。在這種情況下,它使用了. 通常最好使用相反的 ( ) 并切換每個(gè)主體。!=if-else!===


查看完整回答
反對(duì) 回復(fù) 2023-04-25
?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

import string

import random


s1 = input("enter the string:")

n = len(a)

s2 = None

char = string.digits+string.ascii_uppercase+string.ascii_lowercase


c = 1

while s1!=s2:

    s2= ''.join(random.choice(char) for i in range(n))

    c+=1

    print(c,s2)


查看完整回答
反對(duì) 回復(fù) 2023-04-25
?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊

import random

import string



possibleCharacters = string.ascii_lowercase + string.digits + 

string.ascii_uppercase + ' ., !?;:'


a='zero'

c=1

while a != random:

      b = ''.join(random.choice(possibleCharacters)for i in range(len(a)))

      c=c+1

      print(b)

      if b ==a:

         print(a)

         print("target is matched after",c," attermpts")

         break

#try this short and simple


查看完整回答
反對(duì) 回復(fù) 2023-04-25
  • 3 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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