let link = "www.example.com/sfs3/dd/423"
if let url = NSURL(string: link) {
let newLink = url.lastPathComponent
// ...
}
当我打印def game(digits):
five_digit = ""
for x in range(digits):
five_digit = str(random.randint(0, 9))
five_digit += five_digit
时,我会得到five_digit
,22
,88
,55
等的输出。
答案 0 :(得分:2)
这么多问题:
你的变量和函数命名很差;为什么game
会返回一串数字?当字符串是任意位数时,为什么字符串称为five_digit
?
您的缩进在您的问题和解决方案中都已损坏。 现在已修复
重复的字符串连接很慢; str.join
是一种更好的方法(双关语)
尝试改为
from string import digits
from random import choice
def digit_string(length):
return "".join(choice(digits) for _ in range(length))
或者
from random import randint
def digit_string(length):
return "{0:0{1}d}".format(randint(0, 10**length - 1), length)
答案 1 :(得分:1)
def game(digits):
five_digit = ""
for x in range(digits):
digit = str(random.randint(0, 9))
five_digit += digit
我知道我错在哪里,我的for循环覆盖了five_digit
。